java - Using CardLayout for multiple JPanels and nothing displays -
i'm making simple (and bogus) computer power consumption calculator. i'm using card layout put multiple panels in when run it, there's small window not displaying anything. here's long code, put in 1 class.
package my.project; import java.awt.*; import java.awt.event.*; import java.io.ioexception; import java.net.uri; import java.net.urisyntaxexception; import java.text.decimalformat; import javax.swing.*; public class mainproject extends jframe { cardlayout cl; int motherboardp, oddp, hddp, ssdp, fanp, cpup, gpup, monitorp, hoursint; int ramnum, hddnum, ssdnum, fannum, gpunum; double ramp, totalp, powerperyear; public float tariff = (float) 0.2180; public float costrm; jpanel mainpage, secondpage, thirdpage, results; //jpanel panelcont; jlabel title, icon, motherboard, ram, numram, numssd, numhdd, odd, numfan, cpu, gpu, numgpu, monitor, hours, outage, peryear, costyear, watt, kwatt, rm; jbutton start, exit1, exit2, nxt1, bck1, nxt2, bck2, done, bck3, tips, calc; jtextfield numram, numssd, numhdd, numfan, numgpu, hoursuse, outagepc, peryear, cost; jcombobox mboardbox, rambox, oddbox, cpubox, gpubox, monitorbox; public mainproject() { initcomponents(); } public void initcomponents() { settitle("power consumption calculator"); setdefaultcloseoperation(exit_on_close); setsize(500, 400); container contentpane = getcontentpane(); contentpane.setlayout(cl); /* panelcont = new jpanel(); cl = new cardlayout(); */ //contentpane.add(panelcont); /* panelcont.setlayout(cl); panelcont.add(mainpage,"1"); panelcont.add(secondpage,"2"); panelcont.add(thirdpage,"3"); panelcont.add(results,"4"); cl.show(panelcont,"1"); */ mainpage = new jpanel(); secondpage = new jpanel(); thirdpage = new jpanel(); results = new jpanel(); cl.show(contentpane,"1"); contentpane.add("1", mainpage); contentpane.add("2", secondpage); contentpane.add("3", thirdpage); contentpane.add("4", results); title.setfont(new font("tahoma", font.bold, 18)); title.settext("computer power consumption calculator"); motherboard.settext("motherboard:"); ram.settext("ram:"); numram.settext("no. of ram sticks:"); numssd.settext("num. of ssd:"); numhdd.settext("num. of hdd:"); odd.settext("optical disk drive:"); numfan.settext("num. of fans:"); cpu.settext("cpu:"); gpu.settext("gpu:"); numgpu.settext("num. of gpu:"); monitor.settext("monitor:"); hours.settext("hours of usage/day:"); outage.settext("pc power outage:"); peryear.settext("per year:"); costyear.settext("cost/year"); watt.settext("watts (w)"); kwatt.settext("kilowatts(kw)"); rm.settext("rm"); icon.seticon(new javax.swing.imageicon(getclass().getresource("/my/project/pclogo.png"))); borderlayout mainl = new borderlayout(); mainpage.setlayout(mainl); mainpage.add(title, borderlayout.page_start); mainpage.add(icon, borderlayout.center); mainpage.add(start, borderlayout.south); mainpage.add(exit1,borderlayout.south); gridlayout secondl = new gridlayout(0,2); secondpage.setlayout(secondl); secondpage.add(motherboard); secondpage.add(mboardbox); secondpage.add(ram); secondpage.add(rambox); secondpage.add(numram); secondpage.add(numram); secondpage.add(numssd); secondpage.add(numssd); secondpage.add(numhdd); secondpage.add(numhdd); secondpage.add(odd); secondpage.add(oddbox); secondpage.add(numfan); secondpage.add(numfan); secondpage.add(nxt1); secondpage.add(bck1); gridlayout thirdl = new gridlayout(0,2); thirdpage.setlayout(thirdl); thirdpage.add(cpu); thirdpage.add(cpubox); thirdpage.add(gpu); thirdpage.add(gpubox); thirdpage.add(numgpu); thirdpage.add(monitor); thirdpage.add(monitorbox); thirdpage.add(hours); thirdpage.add(hoursuse); thirdpage.add(nxt2); thirdpage.add(bck2); gridlayout resultl = new gridlayout(0,3); results.setlayout(resultl); results.add(outage); results.add(outagepc); results.add(watt); results.add(peryear); results.add(peryear); results.add(kwatt); results.add(costyear); results.add(cost); results.add(rm); results.add(calc); results.add(bck3); results.add(exit2); results.add(tips); start.settext("start"); start.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //startactionperformed(evt); cl.show(contentpane,"2"); } }); exit1.settext("exit"); exit1.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // exit1actionperformed(evt); system.exit(0); } }); exit2.settext("exit"); exit2.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // exit2actionperformed(evt); system.exit(0); } }); nxt1.settext("next >>"); nxt1.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // nxt1actionperformed(evt); cl.show(contentpane,"3"); ramnum = integer.parseint(numram.gettext()); ssdnum = integer.parseint(numssd.gettext()); hddnum = integer.parseint(numhdd.gettext()); fannum = integer.parseint(numfan.gettext()); jcombobox cb1 = (jcombobox)evt.getsource(); string mboard = (string)cb1.getselecteditem(); if("average".equals(mboard)) { motherboardp = 32; } if("high end".equals(mboard)) { motherboardp = 60; } jcombobox cb2 = (jcombobox)evt.getsource(); string ramtype = (string)cb2.getselecteditem(); if("dddr1".equals(ramtype)) { ramp = 5; } if("ddr2".equals(ramtype)) { ramp = 4; } if("ddr3".equals(ramtype)) { ramp = 2.5; } jcombobox cb3 = (jcombobox)evt.getsource(); string typeodd = (string)cb3.getselecteditem(); if("dvd".equals(typeodd)) { oddp = 22; } if("bluray".equals(typeodd)) { oddp = 27; } } }); nxt2.settext("next >>"); nxt2.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // nxt2actionperformed(evt); cl.show(contentpane,"4"); gpunum = integer.parseint(numgpu.gettext()); hoursint = integer.parseint(hoursuse.gettext()); jcombobox cb4 = (jcombobox)evt.getsource(); int incpu = (integer)cb4.getselectedindex(); switch(incpu) { case 0: cpup = 89; break; case 1: cpup = 119; break; case 2: cpup = 127; break; case 3: cpup = 125; break; case 4: cpup = 33; break; case 5: cpup = 98; break; } jcombobox cb5 = (jcombobox)evt.getsource(); int nvgpu = (integer)cb5.getselectedindex(); switch(nvgpu) { case 0: gpup = 295; break; case 1: gpup = 100; break; case 2: gpup = 250; break; case 3: gpup = 150; break; case 4: gpup = 105; break; case 5: gpup = 275; break; } jcombobox cb6 = (jcombobox)evt.getsource(); int msize = (integer)cb6.getselectedindex(); if(msize == 0) { monitorp = 20; } if(msize == 1) { monitorp = 28; } if(msize == 2) { monitorp = 50; } } }); bck1.settext("<< back"); bck1.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // bck1actionperformed(evt); cl.show(contentpane,"1"); } }); bck2.settext("<< back"); bck2.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // bck2actionperformed(evt); cl.show(contentpane,"2"); } }); bck3.settext("<< back"); bck3.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // bck3actionperformed(evt); cl.show(contentpane,"3"); } }); tips.settext("tips on saving energy"); tips.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //tipsactionperformed(evt); try { string url ="http://www.toptenreviews.com/computers/articles/10-computer-energy-saving-tips-go-green/"; desktop dt = desktop.getdesktop(); uri uri = new uri(url); dt.browse(uri.resolve(uri)); } catch (urisyntaxexception | ioexception e) { joptionpane.showmessagedialog(null, e.getmessage()); } } }); calc.settext("calculate"); calc.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // calcactionperformed(evt); gpup *= gpunum; ramp *= ramnum; hddp = hddnum * 5; ssdp = ssdnum * 3; fanp = fannum * 4; totalp = motherboardp + cpup + gpup + ramp + hddp + ssdp + monitorp + fanp; powerperyear = totalp * hoursint * 365; costrm = (float) ((powerperyear / 1000) * tariff); decimalformat df = new decimalformat("#.##"); df.format(costrm); string outage = double.tostring(totalp); string ppy = double.tostring(powerperyear); string price = float.tostring(costrm); outagepc.settext(outage); peryear.settext(ppy); cost.settext(price); } }); mboardbox.setmodel(new defaultcomboboxmodel(new string[] { "average", "high end" })); mboardbox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { // mboardboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); string mboard = (string)cb.getselecteditem(); if("average".equals(mboard)) { motherboardp = 32; } if("high end".equals(mboard)) { motherboardp = 60; } } }); rambox.setmodel(new defaultcomboboxmodel(new string[] { "ddr1", "ddr2", "ddr3" })); rambox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //ramboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); string ramtype = (string)cb.getselecteditem(); if("dddr1".equals(ramtype)) { ramp = 5; } if("ddr2".equals(ramtype)) { ramp = 4; } if("ddr3".equals(ramtype)) { ramp = 2.5; } } }); oddbox.setmodel(new defaultcomboboxmodel(new string[] { "dvd", "bluray" })); oddbox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //oddboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); string typeodd = (string)cb.getselecteditem(); if("dvd".equals(typeodd)) { oddp = 22; } if("bluray".equals(typeodd)) { oddp = 27; } } }); cpubox.setmodel(new defaultcomboboxmodel(new string[] { "intel core i3", "intel core i5", "intel core i7", "amd fx 4350", "amd sempron 64 3500", "amd athlon 64 fx-62" })); cpubox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //cpuboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); int incpu = (integer)cb.getselectedindex(); switch(incpu) { case 0: cpup = 89; break; case 1: cpup = 119; break; case 2: cpup = 127; break; case 3: cpup = 125; break; case 4: cpup = 33; break; case 5: cpup = 98; break; } } }); gpubox.setmodel(new defaultcomboboxmodel(new string[] { "nvidia gtx 500", "nvidia gtx 700", "nvidia gtx titan", "amd radeon hd 2900", "amd radeon hd 3870", "amd radeon r9 fury x" })); gpubox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //gpuboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); int nvgpu = (integer)cb.getselectedindex(); switch(nvgpu) { case 0: gpup = 295; break; case 1: gpup = 100; break; case 2: gpup = 250; break; case 3: gpup = 150; break; case 4: gpup = 105; break; case 5: gpup = 275; break; } } }); monitorbox.setmodel(new defaultcomboboxmodel(new string[] { "17\"-19\"", "20\"-22\"", "24\"-30\"" })); monitorbox.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { //monitorboxactionperformed(evt); jcombobox cb = (jcombobox)evt.getsource(); int msize = (integer)cb.getselectedindex(); if(msize == 0) { monitorp = 20; } if(msize == 1) { monitorp = 28; } if(msize == 2) { monitorp = 50; } } }); } public static void go() { jframe frame = new jframe(); frame.setvisible(true); } public static void main(string[] args) { mainproject.go(); } }
where fix code , how many bad java programming practices have implemented in code?
first...
you need create instance of cardlayout
before apply container , before add components...
cl = new cardlayout(); contentpane.setlayout(cl);
second...
you should using add(component, object)
method
contentpane.add(mainpage, "1");
thrid
public static void go() { jframe frame = new jframe(); frame.setvisible(true); }
does nothing creates empty frame nothing in it. might helpful use mainproject
, since extends jframe
fourth...
don't extend directly jframe
, you're not adding new functionality class, locks single use cases , cause problems 1 you're having.
instead, consider starting jpanel
instead...
public class mainproject extends jpanel {
(ps- change may cause other compiler errors, i'm not going try , fix here)
then simple create new instance of jframe
, add component it...
public static void go() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new mainproject()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); }
fifth
you might want take closer at...
and...
for more ideas , how might better manage cardlayout
you might have @ how use tabbed panes alternative
Comments
Post a Comment