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
   }
有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top