Thursday 15 March 2012

java - How to reference a parameter from a constructor in another class? -



java - How to reference a parameter from a constructor in another class? -

import java.awt.*; import javax.swing.*; import java.text.numberformat; public class productbutton extends jbutton { private string productname; private double productprice; /** creates button display image of product (assumed stored in file starting specified name , ending ".jpg"), specified product name, , specified cost (formatted properly); text displayed below image , centered. @param name product name. @param cost selling cost product. */ public productbutton (string name, double price) { productname = name; productprice = price; imageicon icon = new imageicon(name + ".jpg"); this.seticon(icon); numberformat f = numberformat.getcurrencyinstance(); this.settext(f.format(price)); this.sethorizontaltextposition(jbutton.center); this.setverticaltextposition(jbutton.bottom); } public string getname() { homecoming productname; } public double getprice() { homecoming productprice; } }

driver:

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class snackshopframe extends jframe implements actionlistener{ private productbutton coffeebutton, cocoabutton, donutbutton, cookiebutton, muffinbutton, cupcakebutton; private jbutton newcustomer; private jlabel totalprintout, totalpricestring; private double totalprice; public snackshopframe(){ jframe window = new jframe("snack shop register"); window.setsize(500,700); jpanel first = new jpanel(); jlabel direction = new jlabel("click on products client wishes purchase:"); first.add(direction); first.setlayout(new borderlayout()); first.add(direction, borderlayout.north); jpanel sec = new jpanel(); second.setlayout(new gridlayout(3,2,10,10)); coffeebutton = new productbutton("coffee", 3.75); coffeebutton.addactionlistener(this); second.add(coffeebutton); cocoabutton = new productbutton("cocoa", 2.25); cocoabutton.addactionlistener(this); second.add(cocoabutton); donutbutton = new productbutton("donut",1.50); donutbutton.addactionlistener(this); second.add(donutbutton); cookiebutton = new productbutton("cookie", 1.25); cookiebutton.addactionlistener(this); second.add(cookiebutton); muffinbutton = new productbutton("muffin", 1.75); muffinbutton.addactionlistener(this); second.add(muffinbutton); cupcakebutton = new productbutton("cupcake", 1.50); cupcakebutton.addactionlistener(this); second.add(cupcakebutton); jpanel 3rd = new jpanel(); totalprintout = new jlabel(""); third.add(totalprintout); newcustomer = new jbutton("next customer"); newcustomer.addactionlistener(this); third.add(newcustomer); container contentpane = window.getcontentpane(); contentpane.setlayout(new borderlayout()); contentpane.add(first, borderlayout.north); contentpane.add(second, borderlayout.center); contentpane.add(third, borderlayout.south); window.setvisible(true); window.setdefaultcloseoperation(jframe.exit_on_close); } public void actionperformed(actionevent e){ if(e.getsource() != newcustomer){ if(e.getsource() == coffeebutton){ totalprice += 3.75; } if(e.getsource() == cocoabutton){ totalprice += 2.25; } if (e.getsource() == donutbutton){ totalprice += 1.50; } if (e.getsource() == cookiebutton){ totalprice += 1.25; } if (e.getsource() == muffinbutton){ totalprice += 1.75; } if (e.getsource() == cupcakebutton){ totalprice += 1.50; } } if(e.getsource() == newcustomer ){ totalprice = 0; } totalpricestring = new jlabel(string.valueof(totalprice)); totalprintout.settext("current total: $" + totalprice); } public static void main (string[] args){ new snackshopframe().setvisible(true); } }

how values in driver class (in if statements) formatted using numberformat instance made in first class?

also, when run gui, 2 windows open. 1 blank , 1 actual program. how blank 1 stop opening up?

here 1 way of doing it. not way or efficient, perchance easiest understand. instead of writing

totalpricestring = new jlabel(string.valueof(totalprice)); totalprintout.settext("current total: $" + totalprice);

just utilize same technique used product buttons.

numberformat f = numberformat.getcurrencyinstance(); totalprintout.settext("current total: " + f.format(totalprice));

java user-interface button constructor jframe

No comments:

Post a Comment