Java AWT: Clean / Remove a container panel
Java AWT: Clean / Remove a container panel
I am new to Java AWT, and I'm wanting to do an exercise for college.
This would be the code:
public class Panel0 extends JPanel {
//...
private void createMyLayout(bool test) {
this.add(BorderLayout.CENTER, new Panel1(test));
}
//...
}
public class Panel1 extends JPanel {
public Panel1(bool test){
super(new GridLayou开发者_开发知识库t(1,4));
if(breed!= Breed.ZOMBIE)
add(new Panel2("helow 1 test"));
else
add(new Panel2("helow 2 test"));
}
}
public class Panel2 extends JPanel{
public Panel2(String myText){
super(new BorderLayout());
add(new JLabel(myText));
}
}
When you call the method "createMyLayout" I need to delete the previous layout and recreate it with new data, but do not get it done.
Thanks.
Actually, it's Swing. You can use the method removeAll in the JPanel0 before add new content on it.
I could solve width this example:
BorderLayout layout = panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
This is actually javax.swing, not java.awt.
Also, your question is rather confusing. You say you want to remove a "layout" but java actually has a Layout class that's not what you're talking about. You want to remove COMPONENTS.
myPanel.removeAll(); //removes all the stuff inside
will do the trick. I'd recommend renaming some of your stuff so you don't get confused between Layouts, Panels, and Components.
精彩评论