Question: Java GUI Here are my codes for GUI system. The button cancel and new order did not erase the calculation for previous order. help me.
Java GUI
Here are my codes for GUI system. The button cancel and new order did not erase the calculation for previous order. help me.
btnorder.addActionListener(this);
apple.addActionListener(this);
orange.addActionListener(this);
pear.addActionListener(this);
guava.addActionListener(this);
peach.addActionListener(this);
mango.addActionListener(this);
btnpay.addActionListener(this);
btncancel.addActionListener(this);
txtcharge.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//declare variables using suitable data type into price for each fruit and quantity
double a;
Object obj = e.getSource(); //return the object if any event occurred
double priceapple = 0;
double priceorange = 0;
double pricepear = 0;
double priceguava = 0;
double pricepeach = 0;
double pricemango = 0;
int countapple = 0;
int countorange = 0;
int countpear = 0;
int countguava = 0;
int countpeach = 0;
int countmango = 0;
if(obj == apple) { //count for apples
txtname.setText("Apple"); //fruits name
txtprice.setText("RM 10.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countapple; //for count
priceapple = countapple * 10.0; //count apple price
display.append(" Apple" + " " + countapple++ + " " + "RM" + df.format(priceapple));
}
totalprice += priceapple ; //total price of apple
if(obj == orange) { //count for oranges
txtname.setText("Orange"); //fruit name
txtprice.setText("RM 9.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countorange; //for count
priceorange = countorange * 9.0; //count orange price
display.append(" Orange" + " " + countorange++ + " " + "RM" + df.format(priceorange));
}
totalprice += priceorange; //total price of orange
if(obj == pear) { //count for pears
txtname.setText("Pear"); //fruit name
txtprice.setText("RM 12.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countpear; //for count
pricepear = countpear * 12.0; //count pear price
display.append(" Pear" + " " + countpear++ + " " + "RM" + df.format(pricepear));
}
totalprice += pricepear; //total price of pear
if(obj == guava) { //count for guava
txtname.setText("Guava"); //fruit name
txtprice.setText("RM 7.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countguava; //for count
priceguava = countguava * 7.0; //count guava price
display.append(" Guava" + " " + countguava++ + " " + "RM" + df.format(priceguava));
}
totalprice += priceguava; //total price of guava
if(obj == peach) { //count for peach
txtname.setText("Peach"); //fruit name
txtprice.setText("RM 15.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countpeach; //for count
pricepeach = countpeach * 15.0; //count peach price
display.append(" Peach" + " " + countpeach++ + " " + "RM" + df.format(pricepeach));
}
totalprice += pricepeach; //total price of peach
if(obj == mango) { //count for mango
txtname.setText("Mango"); //fruit name
txtprice.setText("RM 9.00"); //fruit price
txtquantity.setText("1"); //fruit quantity
++countmango; //for count
pricemango = countmango * 9.0; //count mango price
display.append(" Mango" + " " + countmango++ + " " + "RM" + df.format(pricemango));
}
totalprice += pricemango; //total price of mango
String tp = df.format(totalprice);
txtcharge.setText("RM 1.00"); //set price
double t = 1.00;
a = t + totalprice;
txtsubtotal.setText("RM" + tp); //for subtotal using setText
txttotal.setText("RM" + df.format(a)); //for total using setText
if(obj == btnorder) { //for button order
txtname.setText("");
txtprice.setText("");
txtquantity.setText("");
display.setText("");
//txttotal.setText("");
txtsubtotal.setText("");
txtcharge.setText("");
txttotal.setText("");
lblpay.setText("");
}
if(obj == btnpay) { // for button pay
lblpay.setText("The total charge is RM " + df.format(a)); //the set text with total price
}
else if (obj == btncancel) { //for button cancel
txtname.setText("");
txtprice.setText("");
txtquantity.setText("");
display.setText("");
//txttotal.setText("");
txtsubtotal.setText("");
txtcharge.setText("");
txttotal.setText("");
lblpay.setText("");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frushi b = new frushi();
b.setTitle("Frushi");
b.setSize(1000,600);
b.setVisible(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
