java - CardLayout .show() method not working -
hi new enough java , trying learn use card feature in window builder having problem using show method. when click button doesn't change desired card. have been loosely following this tutorial
here code:
public class homescreen { private final string weights = "weights panel"; private final string excercise = "excercise panel"; private final string cardio = "cardio panel"; private jpanel cards; //private cardlayout cardlayout = new cardlayout(); /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { //schedule job event dispatch thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }); } public void addcomponenttopane(container pane) { final jpanel background = new jpanel(); background.setbackground(color.dark_gray); background.setlayout(new cardlayout(0, 0)); jbutton btnhome = new jbutton(); btnhome.setforeground(color.light_gray); btnhome.setfont(new font("tekton pro cond", font.plain, 22)); btnhome.setbackground(color.dark_gray); btnhome.settext("home"); background.add(btnhome, "name_107366949842088"); jpanel cardio = new jpanel(); jpanel exercise = new jpanel(); jpanel weights = new jpanel(); jbutton btnweights_1 = new jbutton("weights"); btnweights_1.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { system.out.println("button pressed"); cardlayout cl = (cardlayout) (cards.getlayout()) ; //cl.next(cards); cl.show(cards, weights); } }); jbutton btncardio = new jbutton("cardio"); /* * add new cards */ cards = new jpanel(new cardlayout()); cards.add(exercise, excercise); exercise.setlayout(new boxlayout(exercise, boxlayout.x_axis)); exercise.add(btnweights_1); exercise.add(btncardio); cards.add(cardio,cardio); cards.add(weights,"weights panel"); jbutton btnlegs = new jbutton("running"); cardio.add(btnlegs); jbutton btnswimming = new jbutton("swimming"); cardio.add(btnswimming); jbutton btncycling = new jbutton("cycling"); cardio.add(btncycling); jbutton btnboxing = new jbutton("boxing"); cardio.add(btnboxing); cards.add(weights); jbutton btnbiceps = new jbutton("biceps"); weights.add(btnbiceps); jbutton btnlegs_1 = new jbutton("legs"); weights.add(btnlegs_1); jbutton btnback = new jbutton("back"); weights.add(btnback); jbutton btnchest = new jbutton("chest"); weights.add(btnchest); pane.add(background, borderlayout.page_start); pane.add(cards,borderlayout.page_end); } public void itemstatechanged(itemevent evt) { cardlayout cl = (cardlayout)(cards.getlayout()); cl.show(cards, (string)evt.getitem()); } /** * create gui , show it. thread safety, * method should invoked * event dispatch thread. */ private static void createandshowgui() { //create , set window. jframe frame = new jframe("cardlayoutdemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); //create , set content pane. homescreen demo = new homescreen(); demo.addcomponenttopane(frame.getcontentpane()); //display window. frame.pack(); frame.setvisible(true); } }
there call add doesn't contain cardlayout
constraint. because components in swing can have 1 parent, line supersedes earlier call did specify constraint.
cards.add(weights);
this statement should removed.
Comments
Post a Comment