java - CardLayout, switch between JPanels by ButtonClick -
i want switch between jpanels clicking buttons on jpanels.
for example: have jpanel sim jbutton simknop , jpanel jbutton helpknop want switch between these 2 jpanels clicking buttons. when click jbutton simknop jpanel should appear , when click jbutton jpanel sim should appear.
below can find different classes:
main.java
public class main extends jframe { jpanel cards; sim sim; help; public main() { this.setsize(1024,768); //this.setlayout(new boxlayout(this, boxlayout.y_axis)); this.setdefaultcloseoperation(jframe.exit_on_close); this.settitle("crazy bombardement"); this.setlocation(800, 100);//standaard in de hoek van het scherm cards = new jpanel(); cards.setlayout(new cardlayout()); sim = new sim(); = new help(); cards.add(sim, "sim"); cards.add(help, "help"); this.add(cards); this.setvisible(true); } public static void main(string[] args) { new main(); }
sim.java
public class sim extends jpanel { jbutton simknop; public sim() { simknop = new jbutton("simknop"); this.add(simknop); this.setbackground(color.black); } }
help.java
public class extends jpanel { jbutton helpknop; public help() { helpknop = new jbutton("helpknop"); this.add(helpknop); this.setbackground(color.red); }
i want use cardlayout can't figure out how make work listen different actionlisteners.
any appreciated!
1) buttons should not on each panel in cardlayout. need on external panel visible, regardless of card showing on card panel.
2) once buttons located correctly, add actionlistener each, actionperformed method (using sim button example):
cardlayout cl = (cardlayout) cards.getlayout(); cl.show(cards, "sim");
further reading: how use cardlayout
edit: in theory, have buttons directly on card panels, opposite button on each panel (i.e., sim button on panel, , vice versa).
Comments
Post a Comment