I've soome buttons in my coding. It has some values. I want to add those val & display in a text box. When u press a button the amt has to be calculated. I tried bt it's not working.How can I do this? I need quick reply. This is my code.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class soda extends Applet implements ActionListener, ItemListener {
Label lblhead,lblfoot,lblsize,lbltype;
Button b1,b2,b3,b4,b5,b6,b7,btnbal;
Choice choice1;
Checkbox ch1,ch2,ch3,ch4,ch5;
double num=0,money;
CheckboxGroup cbg,cbg1;
TextField txtamt,txtbal;
public void init() {
Label lblhead = new Label();
cbg = new CheckboxGroup();
ch1 = new Checkbox("COLA", cbg, false);
ch2 = new Checkbox("ORANGE", cbg, false);
ch3 = new Checkbox("LIME", cbg, false);
Button b1 = new Button();
Button b2 = new Button();
Button b3 = new Button();
Button b4 = new Button();
Button b5 = new Button();
Button b6 = new Button();
Button b7 = new Button();
Choice choice1 = new Choice();
choice1.add("10 oz");
choice1.add("16 oz");
Label lblsize = new Label();
Label lbltype = new Label();
cbg1 = new CheckboxGroup();
ch4 = new Checkbox("Regular", cbg, false);
ch5 = new Checkbox("Diet", cbg, false);
Label lblamt = new Label();
TextField txtamt = new TextField();
Button btnbal = new Button();
TextField txtbal = new TextField();
Button btnref = new Button();
Label lblfoot = new Label();
setBackground(new Color(255, 153, 153));
setLayout(null);
lblhead.setAlignment(Label.CENTER);
lblhead.setFont(new Font("Calibri", 1, 18));
lblhead.setText("Soda Vending Machine");
add(lblhead);
lblhead.setBounds(80, 30, 230, 30);
add(ch1);
ch1.setBounds(60, 70, 58, 20);
add(ch2);
ch2.setBounds(60, 100, 77, 20);
add(ch3);
ch3.setBounds(60, 130, 53, 20);
b1.setBackground(new Color(0, 153, 153));
b1.setLabel("RM 00.10");
add(b1);
b1.setBounds(320, 60, 70, 24);
b2.setBackground(new Color(0, 153, 153));
b2.setLabel("RM 00.20");
add(b2);
b2.setBounds(320, 100, 68, 24);
b3.setBackground(new Color(0, 153, 153));
b3.setLabel("RM 00.50");
add(b3);
b3.setBounds(320, 140, 68, 24);
b4.setBackground(new Color(0, 153, 153));
b4.setLabel("RM 1.00");
add(b4);
b4.setBounds(320, 180, 70, 24);
b5.setBackground(new Color(0, 153, 153));
b5.setLabel("RM 2.00");
add(b5);
b5.setBounds(320, 220, 70, 24);
b6.setBackground(new Color(0, 153, 153));
b6.setLabel("RM 5.00");
add(b6);
b6.setBounds(320, 260, 70, 24);
b7.setBackground(new Color(0, 153, 153));
b7.setLabel("RM 10.00");
add(b7);
b7.setBounds(320, 300, 70, 24);
add(choice1);
choice1.setBounds(200, 170, 90, 20);
lblsize.setText("SIze");
add(lblsize);
lblsize.setBounds(60, 170, 70, 20);
lbltype.setText("Type");
add(lbltype);
lbltype.setBounds(60, 200, 30, 20);
//ch4.setLabel("Regular");
add(ch4);
ch4.setBounds(200, 200, 84, 20);
//ch5.setLabel("Diet");
add(ch5);
ch5.setBounds(200, 230, 48, 20);
lblamt.setText("Total Amount");
add(lblamt);
lblamt.setBounds(60, 270, 76, 20);
add(txtamt);
txtamt.setBounds(200, 270, 90, 20);
btnbal.setBackground(new Color(0, 153, 153));
btnbal.setLabel("Balance");
add(btnbal);
btnbal.setBounds(60, 300, 61, 24);
add(txtbal);
txtbal.setBounds(200, 300, 90, 20);
btnref.setBackground(new Color(0, 153, 153));
btnref.setLabel("Take Soda and Refresh");
add(btnref);
btnref.setBounds(60, 350, 320, 24);
lblfoot.setAlignment(Label.CENTER);
lblfoot.setFont(new Font("Calibri", 1, 14));
lblfoot.setText("Thank You & Come Again!!!");
add(lblfoot);
ch1.addItemListener(this);
ch2.addItemListener(this);
ch3.addItemListener(this);
ch4.addItemListener(this);
ch5.addItemListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
btnbal.addActionListener(this);
}
public void itemStateChanged(ItemEvent ie) {
if(ie.getItemSelectable()==ch1)
{
setBackground(Color.red);
ch5.setVisible(true);
}
if(ie.getItemSelectable()==ch2)
{
setBackground(Color.orange);
ch5.setVisible(true);
}
if(ie.getItemSelectable()==ch3)
{
setBackground(Color.green);
ch5.setVisible(false);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1)
money=0.10;
else if(e.getSource()==b2)
money=0.20;
else if(e.getSource()==b3)
money=0.50;
else if(e.getSource()==b4)
money=1.00;
else if(e.getSource()==b5)
money=2.00;
else if(e.getSource()==b6)
money=5.00;
else if(e.getSource()==b7)
money=10.00;
num=num+money;
txtamt.setText(num+"");
}
}