swing - Manage Java CardLayout JPanels created with different classes -


i need simple example of how manage multiple jpanels (created different classes) in 1 jframe using cardlayout (or else?). illustrative example of need:

panel a:

enter image description here

panel b:

enter image description here

example of file structures:

// gui.java public class gui {     ...     ...     ... }  // panela.java public class panela {     ...     ...     ... () {         jpanel pnl = new jpanel();         pnl.setbackground(color.orange);         jbutton btn = new jbuttn("show panel b");         pnl.add(btn);     }      public void actionperformed(actionevent ae) {         ...     } }  // panelb.java public class panelb {     ...     ...     ... () {         jpanel pnl = new jpanel();         pnl.setbackground(color.green);         jbutton btn = new jbuttn("show panel a");         pnl.add(btn);     }      public void actionperformed(actionevent ae) {         ...     } } 

i found lot of examples doing this, jpanels created in same class jbuttons fields listener access them. tried edit examples no success. sorry bad english, thank in advance!

try this:

gui.java:

public class gui {     ...     jpanel cards = ne jpanel(new cardlayout());     private void initcomponetns() {         ...         // cards container cardlayout...         cards.add(new panela(this));         cards.add(new panelb(this));         ...     } } 

panela.java

public class panela extends jpanel {     ...     private final gui gui;     panela(gui gui) {         this.gui = gui;     }     ... () {         setbackground(color.orange);         jbutton btn = new jbuttn("show panel b");         add(btn);     }      public void actionperformed(actionevent ae) {         ...         gui.cards. ...     } } 

panelb.java

public class panelb extends jpanel {     ...     private final gui gui;     panelb(gui gui) {         this.gui = gui;     }     ...     ... () {         setbackground(color.green);         jbutton btn = new jbuttn("show panel a");         add(btn);     }      public void actionperformed(actionevent ae) {         ...         gui.cards. ...     } } 

edited...


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -