Panel update no longer works when panels are local
I am working on a swing project, and I was following the example here of
how to add a Jpanel by clicking a Jbutton. The example works fine, but I
have one question. If I change the panels to be local variables instead of
global, the MainPanel class now becomes:
class MainPanel extends JPanel {
...
public void checkPanel() {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
if (i == 1) {
this.add(p1);
this.remove(p2);
} else if (i == 2) {
this.add(p2);
this.remove(p1);
}
}
...
}
Yet, when the panels are local variables, the example no longer works as
intended. Why is this?
No comments:
Post a Comment