我对java很新。我的问题是,是否可以使用JButton v,v1?在这个actionPerformed()方法里面?如果有可能,那怎么样? 提前谢谢。
PlaceOrder.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PlaceOrder extends JFrame implements ActionListener
{
JButton confirm,cancel,logOut;
JRadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8;
JTextField r1,r2,r3,r4,r5,r6,r7,r8;
JLabel Item,Quantity;
Food f[] = new Food[10];
double total = 0;
public PlaceOrder()
{
f[0] = new Food("Chicken Burger",120);
f[1] = new Food("Chicken BBQ Burger",150);
f[2] = new Food("Chicken BBQ Cheese Burger",170);
f[3] = new Food("Beef Burger",130);
f[4] = new Food("Beef BBQ Burger",160);
f[5] = new Food("Pizza",180);
f[6] = new Food("Coffee",80);
f[7] = new Food("Soft Drinks(Can)",35);
setSize(700,700);
setVisible(true);
setLayout(null);
rb1 = new JRadioButton("Chicken Burger");
rb2 = new JRadioButton("Chicken BBQ Burger");
rb3 = new JRadioButton("Chicken BBQ Cheese Burger");
rb4 = new JRadioButton("Beef Burger");
rb5 = new JRadioButton("Beef BBQ Burger");
rb6 = new JRadioButton("Pizza");
rb7 = new JRadioButton("Coffee");
rb8 = new JRadioButton("Soft Drinks(Can)");
rb1.setBounds(150,100,150,30);
add(rb1);
rb2.setBounds(150,130,150,30);
add(rb2);
rb3.setBounds(150,160,150,30);
add(rb3);
rb4.setBounds(150,190,150,30);
add(rb4);
rb5.setBounds(150,220,150,30);
add(rb5);
rb6.setBounds(150,250,150,30);
add(rb6);
rb7.setBounds(150,280,150,30);
add(rb7);
rb8.setBounds(150,310,150,30);
add(rb8);
r1 = new JTextField();
r2 = new JTextField();
r3 = new JTextField();
r4 = new JTextField();
r5 = new JTextField();
r6 = new JTextField();
r7 = new JTextField();
r8 = new JTextField();
Item = new JLabel("Item");
Quantity = new JLabel("Quantity");
Item.setBounds(160,20,100,50);
add(Item);
Quantity.setBounds(360,20,100,50);
add(Quantity);
r1.setBounds(350,100,150,20);
add(r1);
r2.setBounds(350,130,150,20);
add(r2);
r3.setBounds(350,160,150,20);
add(r3);
r4.setBounds(350,190,150,20);
add(r4);
r5.setBounds(350,220,150,20);
add(r5);
r6.setBounds(350,250,150,20);
add(r6);
r7.setBounds(350,280,150,20);
add(r7);
r8.setBounds(350,310,150,20);
add(r8);
confirm = new JButton("Order Status");
cancel = new JButton("Cancel Order");
logOut = new JButton("LogOut");
confirm.setBounds(150,500,120,30);
add(confirm);
cancel.setBounds(380,500,120,30);
add(cancel);
logOut.setBounds(550,50,80,30);
add(logOut);
confirm.addActionListener(this);
cancel.addActionListener(this);
logOut.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == confirm)
{
JFrame YourOrder = new JFrame();
YourOrder.setSize(700,800);
YourOrder.setLayout(null);
YourOrder.setVisible(true);
JLabel confirmation = new JLabel("Your Order");
confirmation.setBounds(250,30,150,30);
YourOrder.add(confirmation);
JLabel s[] = new JLabel[3];
s[0] = new JLabel("Item");
s[0].setBounds(100,100,50,30);
YourOrder.add(s[0]);
s[1] = new JLabel("Quantity");
s[1].setBounds(250,100,50,30);
YourOrder.add(s[1]);
s[2] = new JLabel("Total");
s[2].setBounds(400,100,50,30);
YourOrder.add(s[2]);
JLabel l,l1,l2;
JLabel netTotal = new JLabel("Net Amount = ");
netTotal.setBounds(400,550,100,30);
YourOrder.add(netTotal);
JButton v,v1;
v = new JButton("Change");
v.setBounds(100,600,100,30);
YourOrder.add(v);
v.addActionListener(this);
v1 = new JButton("Confirm Order");
v1.setBounds(400,600,120,30);
YourOrder.add(v1);
v1.addActionListener(this);
if(rb1.isSelected() && r1.getText() != "")
{
total = total + ((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice()));
l = new JLabel(rb1.getText());
l.setBounds(100,150,100,30);
YourOrder.add(l);
l1 = new JLabel(r1.getText());
l1.setBounds(250,150,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice())));
l2.setBounds(400,150,100,30);
YourOrder.add(l2);
}
if(rb2.isSelected() && r2.getText() != "")
{
total = total + ((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice()));
l = new JLabel(rb2.getText());
l.setBounds(100,200,100,30);
YourOrder.add(l);
l1 = new JLabel(r2.getText());
l1.setBounds(250,200,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice())));
l2.setBounds(400,200,100,30);
YourOrder.add(l2);
}
if(rb3.isSelected() && r3.getText() != "")
{
total = total + ((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice()));
l = new JLabel(rb3.getText());
l.setBounds(100,250,100,30);
YourOrder.add(l);
l1 = new JLabel(r3.getText());
l1.setBounds(250,250,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice())));
l2.setBounds(400,250,100,30);
YourOrder.add(l2);
}
if(rb4.isSelected() && r4.getText() != "")
{
total = total + ((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice()));
l = new JLabel(rb4.getText());
l.setBounds(100,300,100,30);
YourOrder.add(l);
l1 = new JLabel(r4.getText());
l1.setBounds(250,300,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice())));
l2.setBounds(400,300,100,30);
YourOrder.add(l2);
}
if(rb5.isSelected() && r5.getText() != "")
{
total = total + ((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice()));
l = new JLabel(rb5.getText());
l.setBounds(100,350,100,30);
YourOrder.add(l);
l1 = new JLabel(r5.getText());
l1.setBounds(250,350,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice())));
l2.setBounds(400,350,100,30);
YourOrder.add(l2);
}
if(rb6.isSelected() && r6.getText() != "")
{
total = total + ((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice()));
l = new JLabel(rb6.getText());
l.setBounds(100,400,100,30);
YourOrder.add(l);
l1 = new JLabel(r6.getText());
l1.setBounds(250,400,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice())));
l2.setBounds(400,400,100,30);
YourOrder.add(l2);
}
if(rb7.isSelected() && r7.getText() != "")
{
total = total + ((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice()));
l = new JLabel(rb7.getText());
l.setBounds(100,450,100,30);
YourOrder.add(l);
l1 = new JLabel(r7.getText());
l1.setBounds(250,450,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice())));
l2.setBounds(400,450,100,30);
YourOrder.add(l2);
}
if(rb8.isSelected() && r8.getText() != "")
{
total = total + ((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice()));
l = new JLabel(rb8.getText());
l.setBounds(100,500,100,30);
YourOrder.add(l);
l1 = new JLabel(r8.getText());
l1.setBounds(250,500,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice())));
l2.setBounds(400,500,100,30);
YourOrder.add(l2);
}
JLabel pay = new JLabel(String.valueOf(total));
pay.setBounds(500,550,100,30);
YourOrder.add(pay);
if(a.getSource() == v)
{
YourOrder.setVisible(false);
}
else if(a.getSource() == v1)
{
JOptionPane.showMessageDialog(this,"Printing Invoice ...");
}
}
else if(a.getSource() == cancel)
{
this.setVisible(false);
PlaceOrder o1 = new PlaceOrder();
}
else
{
this.setVisible(false);
}
}
}
Food.java
class Food
{
private String foodName;
private double foodPrice;
public Food(String foodName,double foodPrice)
{
this.foodName = foodName;
this.foodPrice = foodPrice;
}
public String getFoodName()
{
return this.foodName;
}
public double getFoodPrice()
{
return this.foodPrice;
}
}
这两个班..