java - Trying to use cards layout, but cant make show work -
im trying to use cards layout, , have 2 buttons @ the top supoose change card reason wont work, next method works show or first\last doesnt, ofcourse cant use next, cause want specific card every button, here code:
cards = new cardlayout(); cardpanel = new jpanel(); cardpanel.setlayout(cards); cards.show(cardpanel, "gapas"); jpanel firstcard = new jpanel(); firstcard.setbackground(color.white);; jpanel secondcard = new jpanel(); secondcard.setbackground(color.blue); cardpanel.add(firstcard, "kalam"); cardpanel.add(secondcard, "gapan"); guiframe.add(tabspanel,borderlayout.north); guiframe.add(cardpanel,borderlayout.center); guiframe.setvisible(true); } actionlistener action = new actionlistener() { @override public void actionperformed(actionevent e) { if(e.getactioncommand().matches("kalam")){ cards.show(cardpanel,"kalam"); system.out.println("kalam"); } else{ cards.show(cardpanel, "gapas"); system.out.println("gapas"); } } };
i think want this.
public class testcard extends jframe implements actionlistener { cardlayout cards; jpanel cardpanel, tabspanel; jbutton b1, b2; public testcard() { b1= new jbutton("kalam"); b2= new jbutton("gapas"); tabspanel = new jpanel(); cards = new cardlayout(); cardpanel = new jpanel(); cardpanel.setlayout(cards); jpanel firstcard = new jpanel(); firstcard.setbackground(color.white); jpanel secondcard = new jpanel(); secondcard.setbackground(color.blue); cardpanel.add(firstcard, "kalam"); cardpanel.add(secondcard, "gapas"); tabspanel.add(b1); tabspanel.add(b2); add(tabspanel, borderlayout.north); add(cardpanel, borderlayout.center); b1.addactionlistener(this); b2.addactionlistener(this); setsize(800, 600); cards.show(cardpanel, "gapas"); setvisible(true); } @override public void actionperformed(actionevent e) { if (e.getactioncommand().matches("kalam")) { cards.show(cardpanel, "kalam"); system.out.println("kalam"); } else { cards.show(cardpanel, "gapas"); system.out.println("gapas"); } } public static void main(string[] args) { new testcard(); } }
Comments
Post a Comment