2017-05-03 62 views
0

我正在学习如何创建GUI并需要帮助理解actionListeners。一切都编码,但出于某种原因,我的计算按钮实际上并没有打开与主窗口的结果的新窗口。该计划是一家三明治店,可让您制作三明治并选择饮料。当用户完成后,他们点击计算,并打开一个新的窗口,其中包含小计,税金和最终总计以及另一个退出按钮。这是迄今为止我所有的代码。我很感激任何人都可以提供的指导。Action Listener用结果打开新窗口

三明治店

/** 
* 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); 
} 

} 

回答

1

ÿ OU只是忘了一行: setVisible(true);

你加它在CalculateListener类的actionPerformed(...)方法的底部。

+0

非常感谢你,我不能相信这件事很简单。我一直在调试这个东西几个小时,试图弄清楚为什么它不起作用。谢谢 –

+0

我很高兴能帮上忙!我有很多像这样的愚蠢错误:) – Adrian

0

看来你正在使用主应用程序中的不同帧以及CalculateListener,因此需要使用侦听器来计算值并将其设置在可以更新UI的主类上。尝试检查MVC模式,并尝试在变量名称上使用Java命名标准,如不带_,并以小写字母开头。