Hi there,
I did this program, but, im having problems with attaching the events. i want the total bill amount to be calculated when i click the ‘OK’ button, and i need it to be displayed in the window. The problem is that I dont know why I dont get why its not adding the total bill of what i selected. It only displays "Total: 0"
im assuming that:
Sandwich = $2 per unit
Tea – $1 per unit
Coke – $1 per unit
Thanks in advance and i hope you guys can help me out,
cheers
outofmymind
I did this program, but, im having problems with attaching the events. i want the total bill amount to be calculated when i click the ‘OK’ button, and i need it to be displayed in the window. The problem is that I dont know why I dont get why its not adding the total bill of what i selected. It only displays "Total: 0"
im assuming that:
Sandwich = $2 per unit
Tea – $1 per unit
Coke – $1 per unit
Code:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Cafeamr extends JApplet implements ActionListener{
private JButton b1, b2;
private JCheckBox c1, c2, c3;
//private JRadioButton c5, c6, c7;
private JComboBox q;
private JLabel label1, label2,label3,label4,label5;
private JPanel p1, p2, p3, p4, p5, p6, p7, p8;
private JTextField textbox1,textbox2,textbox3;
public Cafe()
{
// build left north panel
label1 = new JLabel( "Cafe: My Cafe" );
p1 = new JPanel();
p1.setLayout( new FlowLayout( FlowLayout.LEFT ) );
p1.add( label1 );
// build right east panel
b1 = new JButton( "Ok" );
b1.addActionListener(this);
b2 = new JButton( "Cancel" );
b2.addActionListener(this);
p2 = new JPanel();
p2.setLayout( new GridLayout( 2, 1, 5, 5 ) );
p2.add( b1 );
p2.add( b2 );
// build left south panel
label2 = new JLabel( "Payment Mode: " );
q = new JComboBox();
q.addItem( "Cash" );
q.addItem("Credit Card");
p3 = new JPanel();
p3.setLayout( new FlowLayout( FlowLayout.LEFT, 10, 0 ) );
p3.add( label2 );
p3.add( q );
// build left east panel
label3=new JLabel("Item");
c1 = new JCheckBox( "Sandwich" );
c2 = new JCheckBox( "Tea" );
c3 = new JCheckBox( "Coke" );
p4 = new JPanel();
p4.setLayout( new GridLayout(4,1,3,3 ) );
p4.add(label3);
p4.add(c1);
p4.add(c2);
p4.add(c3);
// build left west panel
p5 = new JPanel();
p5.setLayout( new GridLayout(4,1,5,5) );
label4=new JLabel("Quantity");
textbox1=new JTextField(2);
textbox2=new JTextField(2);
textbox3=new JTextField(2);
p5.add(label4);
p5.add(textbox1);
p5.add(textbox2);
p5.add(textbox3);
// build left center
p8 = new JPanel();
p8.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 0 ) );
// p8.setBackground( Color.white );
p8.add( p4 );
p8.add( p5 );
// setup left panel
p6 = new JPanel();
p6.setLayout( new BorderLayout() );
p6.add( p1, BorderLayout.NORTH );
p6.add( p8, BorderLayout.CENTER );
p6.add( p3, BorderLayout.SOUTH );
// setup applet layout
p7 = new JPanel();
p7.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 5 ) );
p7.add( p6 );
p7.add( p2 );
getContentPane().add( p7 );
setSize( 400, 250 );
show();
}
public void actionPerformed (ActionEvent d)
{
if(d.getSource() ==b1){
int total= 0;
int SP = 2;
int TP = 1;
int CP = 1;
//if (d.getSource())
try
{
if (c1.isSelected()==true){
String B1 = textbox1.getText();
int b1 = Integer.parseInt(B1);
total = total + (b1 * SP);
}
if (c2.isSelected()==true){
String B2 = textbox2.getText();
int b2 = Integer.parseInt(B2);
total = total + (b2 * TP);
}
if (c3.isSelected()==true){
String B3 = textbox1.getText();
int b3 = Integer.parseInt(B3);
total = total + (b3 * CP);
}
}
catch(Exception e) {
e.printStackTrace();
}
label5 = new JLabel( "Total: "+ total );
p7.add(label5);
validate();
System.out.println(total);
} // ok button
else {
System.exit(1);
}
}
public void init()
{
new Cafe();
}
}
cheers
outofmymind
Comment