【发布时间】:2018-09-24 18:48:06
【问题描述】:
我已经检查过How can I remove a JPanel from a JFrame?,但是当我尝试做frame.remove(mainScreen) 时,整个事情就僵住了。我也试过mainScreen.setVisible(false);但这显示了球,但它们是静止的(它们应该移动)。
// important variables
FlowLayout fL = new FlowLayout();
// create frame
JFrame frame = new JFrame("Ball");
frame.setTitle("GaoMolecules");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false);
Rectangle fDim = frame.getBounds(); // frameDimensions
// add panel to frame
JPanel mainScreen = new JPanel();
mainScreen.setLayout(fL);
fL.setAlignment(FlowLayout.TRAILING);
frame.add(mainScreen);
// other panel components
JTextField numBallsField = new JTextField(4);
JLabel numBallsLabel = new JLabel("Enter the number of balls to generate");
JButton startButton = new JButton("START");
mainScreen.add(numBallsLabel);
mainScreen.add(numBallsField);
mainScreen.add(startButton);
// add listener to button
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//System.out.println("Button Working!");
mainScreen.setVisible(false);
frame.add(new DrawManager());
start = true;
}
});
// this needs to be the very LAST
frame.setVisible(true);
【问题讨论】: