okay know question on jpanels in jpanels has been answered i've studied , i'm confident doesn't answer problem.
i'm creating gui using card layout swap between screens , after processes want panel displays processes panel had shown
here simplified (runnable) version of code problem:
import javax.swing.jframe; public class center { public gui frame = new gui(); public logic dothething = new logic(); public int passtheparcel = -1; public center(){ frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(400,300); frame.setvisible(true); /*i know compairing identical terms won't loop forever passtheparcel change*/ while (-1 == passtheparcel){ if(frame.panel2pressed == true){ passtheparcel = dothething.makerandom(); frame.refreshpanel2(passtheparcel); } frame.repaint(); } } public static void main(string[] args) { center example = new center(); } }
the gui:
import java.awt.cardlayout; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class gui extends jframe { public static jpanel contentpanel = new jpanel(new cardlayout()); public jpanel panel1 = new jpanel(); public jpanel panel2 = new jpanel(); public jpanel panel3 = new jpanel(); public jbutton gotopanel1, gotopanel2, gotopanel3; public jlabel info, panel3label; public boolean panel2pressed = false; public static cardlayout cardlayout = (cardlayout)contentpanel.getlayout(); gridbagconstraints gbc = new gridbagconstraints(); public gui() { super("test"); panel1.setlayout(new gridbaglayout()); panel2.setlayout(new gridbaglayout()); panel3.setlayout(new gridbaglayout()); gbc.insets = new insets(20, 20, 0, 0); //populating first panel gotopanel2 = new jbutton("go panel2"); gbc.gridx = 0; gbc.gridy = 1; panel1.add(gotopanel2, gbc); gotopanel3 = new jbutton("go panel3"); gbc.gridx = 0; gbc.gridy = 2; panel1.add(gotopanel3, gbc); //populate second panel info = new jlabel("place holder"); gbc.gridx = 0; gbc.gridy = 1; panel2.add(info, gbc); //populate third panel panel3label = new jlabel("this panel3"); gbc.gridx = 0; gbc.gridy = 1; panel3.add(panel3label, gbc); gotopanel1 = new jbutton("go panel1"); gbc.gridx = 0; gbc.gridy = 2; panel3.add(gotopanel1, gbc); //create , add action listener buttonhandler handler = new buttonhandler(); gotopanel1.addactionlistener(handler); gotopanel2.addactionlistener(handler); gotopanel3.addactionlistener(handler); // set card layout getcontentpane().add(contentpanel); contentpanel.add(panel1, "panel1"); contentpanel.add(panel2, "panel2"); contentpanel.add(panel3, "panel3"); cardlayout.show(contentpanel, "panel1"); } public void refreshpanel2(int random){ //setting text in label random number info.settext((""+random)); if(random > 25){ gbc.gridx = 0; gbc.gridy = 2; panel2.add(panel3,gbc); panel2.revalidate(); panel2.repaint(); } } public class buttonhandler implements actionlistener{ @override public void actionperformed(actionevent e) { if (e.getsource() == gotopanel1){ cardlayout.show(contentpanel, "panel1"); } if (e.getsource() == gotopanel2){ panel2pressed = true; cardlayout.show(contentpanel, "panel2"); } if (e.getsource() == gotopanel3){ cardlayout.show(contentpanel, "panel3"); } } } }
and logic class:
/*this class may seem useless represents larger piece of code function return panel2 can use later */ import java.util.random; public class logic { public random random = new random(); public int randomnumber; public int makerandom(){ randomnumber = random.nextint(100); system.out.println(randomnumber); return randomnumber; } }
my problem thus: when gotopanel2 button pressed, indeed show panel2 label changed random number when number greater 25 panel3 not displayed. please run , advise.
Comments
Post a Comment