Reputation: 145
JFrame frame1 = new JFrame("frame");
frame1.setVisible(true);
frame1.setPreferredSize(new Dimension(800, 600));
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();
frame1.add(Panel1,BorderLayout.SOUTH);
frame1.add(Panel2,BorderLayout.North);
How do i do something like this that if something happens the frame is blank
if(SOMETHINGHAPPENS)
{
//remove all panels from frame 1 so i have a blank frame
//now i want to add some new panels
}
Upvotes: 1
Views: 566
Reputation: 347184
Simple answer, don't.
Instead, use a CardLayout
on the JFrame
This will allow you to setup a series of "views" which you can switch between as needed
Upvotes: 4
Reputation: 811
Try this. Jframe.remove(JPanel)
. Then call JFrame.pack()
, and possibly JFrame.repaint()
in order to refresh the graphics if necessary.
JPanel extends JComponent, so it should work just fine. Remember to substitute JFrame and JPanel in the code above with the names of the frames/panels you are using the methods on.
Upvotes: 0