java - how to display different screen -
i'm writing simple program using cardlayout. main screen should display button when pressed go next screen contains button screen. problem when run program screen black. tried following tutorials online write own program don't seem find problem code. don't errors when run. here code
//using cardlayout change screen when action performed import javax.swing.jframe; import javax.swing.jtextfield; import javax.swing.jlabel; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.popup; import javax.swing.joptionpane; import java.awt.borderlayout; import java.awt.cardlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.flowlayout; public class cl extends jframe { jpanel cardpanel; jpanel cardpanela; jpanel cardpanelb;//to set different screens cardlayout cl; private jbutton button1; private jbutton button2; private jbutton change; private jlabel label; private jtextfield textfield1; private jtextfield textfield2; jbutton button; public cl() { super("this sample"); cardpanel = new jpanel(); cardpanela = new jpanel(); cardpanelb = new jpanel(); cl = new cardlayout(); cardpanel.setlayout(cl); button1 = new jbutton("button1"); button2 = new jbutton("button2"); change = new jbutton("change screen"); label = new jlabel("this label"); textfield1 = new jtextfield(10); textfield2 = new jtextfield("enter text", 6); cardpanela.add(change); cardpanela.add(label); cardpanela.add(textfield1); cardpanela.add(textfield2); cardpanelb.add(button1); cardpanelb.add(button2); cardpanel.add(cardpanela); cardpanel.add(cardpanelb); jpanel panel1 = new jpanel(); button = new jbutton("initial button"); panel1.add(button); thehandler handler = new thehandler();//make action listener change.addactionlistener(handler); button1.addactionlistener(handler); button2.addactionlistener(handler); button.addactionlistener(handler); /* getcontentpane().add(panel1, borderlayout.north); getcontentpane().add(cardpanela, borderlayout.north); getcontentpane().add(cardpanelb, borderlayout.north); */ } private class thehandler implements actionlistener { public void actionperformed(actionevent event) { if (event.getsource() == button) { cl.show(cardpanel, "panel a"); } if (event.getsource() == change) { cl.show(cardpanelb, "panelb"); } if (event.getsource() == button2) { cl.show(cardpanel, "panela"); } if (event.getsource() == button1) { joptionpane.showmessagedialog(null, "this second screen"); } } } }
/*way use cardlayout: create cardlayout manager , create bunch of different jpanels * each different screen. make panel stores cardlayout layout. * add different elements each panel(buttons, textfields) , add panels jpanel stores * cardlayout */
import javax.swing.jframe; public class cardtest { public static void main(string[] args) { cl object = new cl(); object.setdefaultcloseoperation(jframe.exit_on_close); object.setsize(400, 400); object.setvisible(true); } }
it might simple i'm not sure of is. advice appreciated.
make sure add panels frame
add(cardpanel);
without no components shown
Comments
Post a Comment