java - JButton not displaying -
hi know why "button1" not displaying? can not seem figure out when execute program works , runs not display button. appreciated thanks.
private container c; private jpanel gridpanel; private jcombobox combo; final jlabel label = new jlabel(); private jbutton button1 = new jbutton("clear"); private jbutton button2 = new jbutton("exit"); /** * christopher haddad - 559815x */ public planets() { c = getcontentpane(); gridpanel = new jpanel(); gridpanel.setlayout(new gridlayout(5, 0, 0, 0)); label.setvisible(true); combo = new jcombobox(); combo.seteditable(false); combo.additem("no planet selected"); combo.additem("mercury"); combo.additem("venus"); combo.additem("earth"); gridpanel.add(combo); add(button1); add(button2); button1.addactionlistener(this); button2.addactionlistener(this); c.add(gridpanel, borderlayout.north); settitle("planet diameter"); setsize(700, 250); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); combo.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { jcombobox combobox = (jcombobox) event.getsource(); object select = combobox.getselecteditem(); if(select.tostring().equals("no planet selected")) label.settext(""); else if(select.tostring().equals("mercury")) label.settext("the planet mercury 3100kms in diameter"); else if(select.tostring().equals("venus")) label.settext("the planet venus 7500kms in diameter"); else if (select.tostring().equals("earth")) label.settext("the planet earth 8000kms in diameter"); } }); getcontentpane().add(combo); getcontentpane().add(label); } // event handling method, implementing actionperformed method of actionlistener public void actionperformed(actionevent e) { // set button label number of times has been clicked if(e.getsource() == button1) { label.settext(" "); } else if(e.getsource() == button2) { system.exit(0); } }
it difficult sure, assume adding content directly top level container, jframe
jframe
uses borderlayout
it's default layout manager, using
add(button1); add(button2);
basially says, add
button1
center
position add
button2
center
position. borderlayout
allow single component exist @ specific location.
try adding buttons panel first...
Comments
Post a Comment