我已经检查了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);
答案 0 :(得分:0)
每次您执行remove()时,都应致电
frame.validate();
frame.repaint();