我正在学习如何创建GUI,并且需要了解actionListeners的帮助。一切都进行了编码,但由于某种原因,我的“计算按钮”实际上并未打开具有主窗口结果的新窗口。该程序是一家三明治店,可让您创建三明治并选择饮料。当用户完成操作后,他们单击“计算”,并假设要打开一个新窗口,其中包含小计,税项和最终总计,以及另一个退出按钮。这是我到目前为止的所有代码。任何人都可以提供任何指导,我将不胜感激。
三明治店
/**
* Creating a menu for a sandwich shop. There are 2 choices for bread, 4
choices for fixings, and 4 drink selections. The total will be calculated in
a new
* window using the actionListener. This new window class is
CalculateListener.
* @author Chad Hoye
* @version 1.0
*/
public class SandShop2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private final ButtonGroup buttonGroup_Bread = new ButtonGroup();
private final ButtonGroup buttonGroup_Drink = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SandShop2 frame = new SandShop2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SandShop2() {
String breadOP;
String drinkOP = null;
boolean hamOP = false, turkeyOP = false, cheeseOP = false, mayoOP = false;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 240, 240);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel bread = new JPanel();
bread.setBorder(new LineBorder(new Color(0, 0, 0)));
contentPane.add(bread, BorderLayout.WEST);
bread.setLayout(new GridLayout(2, 1, 0, 0));
JRadioButton rdbtnWhite = new JRadioButton("White");
buttonGroup_Bread.add(rdbtnWhite);
rdbtnWhite.setSelected(true);
bread.add(rdbtnWhite);
JRadioButton rdbtnWheat = new JRadioButton("Wheat");
buttonGroup_Bread.add(rdbtnWheat);
bread.add(rdbtnWheat);
if(rdbtnWhite.isSelected()){
breadOP = "white";
}else{
breadOP = "wheat";
}
JPanel fixings = new JPanel();
fixings.setBorder(new LineBorder(new Color(0, 0, 0)));
contentPane.add(fixings, BorderLayout.CENTER);
fixings.setLayout(new GridLayout(4, 1, 0, 0));
JCheckBox chckbxHam = new JCheckBox("Ham");
fixings.add(chckbxHam);
JCheckBox chckbxTurkey = new JCheckBox("Turkey");
fixings.add(chckbxTurkey);
JCheckBox chckbxCheese = new JCheckBox("Cheese");
fixings.add(chckbxCheese);
JCheckBox chckbxMayo = new JCheckBox("Mayo");
fixings.add(chckbxMayo);
if(chckbxHam.isSelected()){
hamOP = true;
}
if(chckbxTurkey.isSelected()){
turkeyOP = true;
}
if(chckbxCheese.isSelected()){
cheeseOP = true;
}
if(chckbxMayo.isSelected()){
mayoOP = true;
}
JPanel drinks = new JPanel();
drinks.setBorder(new LineBorder(new Color(0, 0, 0)));
contentPane.add(drinks, BorderLayout.EAST);
drinks.setLayout(new GridLayout(4, 1, 0, 0));
JRadioButton rdbtnSoda = new JRadioButton("Soda");
buttonGroup_Drink.add(rdbtnSoda);
drinks.add(rdbtnSoda);
JRadioButton rdbtnBeer = new JRadioButton("Beer");
buttonGroup_Drink.add(rdbtnBeer);
drinks.add(rdbtnBeer);
JRadioButton rdbtnTea = new JRadioButton("Tea");
buttonGroup_Drink.add(rdbtnTea);
drinks.add(rdbtnTea);
JRadioButton rdbtnWater = new JRadioButton("Water");
buttonGroup_Drink.add(rdbtnWater);
drinks.add(rdbtnWater);
if(rdbtnSoda.isSelected()){
drinkOP = "Soda";
}else if(rdbtnBeer.isSelected()){
drinkOP = "Beer";
}else if(rdbtnTea.isSelected()){
drinkOP = "Tea";
}else {
drinkOP = "Water";
}
JPanel Cal_Exit = new JPanel();
Cal_Exit.setBorder(new LineBorder(new Color(0, 0, 0)));
contentPane.add(Cal_Exit, BorderLayout.SOUTH);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new CalculateListener(breadOP, drinkOP, hamOP, turkeyOP, cheeseOP, mayoOP));
Cal_Exit.add(btnCalculate);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ExitListener());
Cal_Exit.add(btnExit);
JLabel lblChadsSandwhichShop = new JLabel("Chad's Sandwhich Shop");
lblChadsSandwhichShop.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblChadsSandwhichShop, BorderLayout.NORTH);
}
}
计算监听器类
public class CalculateListener extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private double subtotal;
private double whiteBread, wheatBread;
private double ham, turkey, cheese, mayo;
private double soda, beer, tea, water;
public CalculateListener(String bread, String drink, boolean hamOP, boolean turkeyOP, boolean cheeseOP, boolean mayoOP){
subtotal = 0;
whiteBread = 1.00;
wheatBread = 1.25;
ham = 0.75;
turkey = 0.65;
cheese = 0.50;
mayo = 0.25;
soda = 0.65;
beer = 3.75;
tea = 2.25;
water = 0.0;
if(bread.equals("white")){
subtotal += whiteBread;
}else{
subtotal += wheatBread;
}
if(drink.equals("Soda")){
subtotal += soda;
}else if(drink.equals("Beer")){
subtotal += beer;
}else if(drink.equals("Tea")){
subtotal += tea;
}else if(drink.equals("Water")){
subtotal += water;
}
if(hamOP)
subtotal += ham;
if(turkeyOP)
subtotal += turkey;
if(cheeseOP)
subtotal += cheese;
if(mayoOP)
subtotal += mayo;
}
public void actionPerformed(ActionEvent e){
setTitle("Your Bill");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(3, 1, 0, 0));
JLabel lblSubtotal = new JLabel(" Subtotal: $" + subtotal);
panel.add(lblSubtotal);
JLabel lblTax = new JLabel("Tax: $" + (subtotal * 0.07));
panel.add(lblTax);
JLabel lblTotal = new JLabel("Total: $" + ((subtotal * 0.07) + subtotal));
panel.add(lblTotal);
JButton btnOk = new JButton("OK");
contentPane.add(btnOk, BorderLayout.SOUTH);
btnOk.addActionListener(new ExitListener());
}
}
退出监听器类
public class ExitListener implements ActionListener {
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
最佳答案
您只是忘记了一行:setVisible(true);
您将其添加到actionPerformed(...)
类中CalculateListener
方法的底部。