Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 操作侦听器打开包含结果的新窗口_Java - Fatal编程技术网

Java 操作侦听器打开包含结果的新窗口

Java 操作侦听器打开包含结果的新窗口,java,Java,我正在学习如何创建GUI,需要帮助理解actionListeners。所有内容都已编码,但由于某些原因,我的“计算”按钮实际上没有打开包含主窗口结果的新窗口。该计划是一个三明治店,允许您创建一个三明治和选择一种饮料。当用户完成后,他们单击“计算”,它将打开一个新窗口,其中包含小计、税金和最终合计,以及另一个退出按钮。这是我到目前为止所有的代码。我希望任何人都能提供指导 三明治店 计算侦听器类 退出侦听器类 你只是忘了一行: 设置VisibleTrue 您将其添加到已执行操作的底部。。。方法。似乎

我正在学习如何创建GUI,需要帮助理解actionListeners。所有内容都已编码,但由于某些原因,我的“计算”按钮实际上没有打开包含主窗口结果的新窗口。该计划是一个三明治店,允许您创建一个三明治和选择一种饮料。当用户完成后,他们单击“计算”,它将打开一个新窗口,其中包含小计、税金和最终合计,以及另一个退出按钮。这是我到目前为止所有的代码。我希望任何人都能提供指导

三明治店

计算侦听器类

退出侦听器类


你只是忘了一行: 设置VisibleTrue


您将其添加到已执行操作的底部。。。方法。

似乎您使用的是来自主应用程序和计算器的不同框架。您需要使用侦听器计算值,并在主类上设置它,以便更新UI。试着检查MVC模式,试着使用Java命名标准,比如不使用变量名,从小写开始。

非常感谢,我不敢相信它会这么简单。我已经调试这个东西好几个小时了,试图找出它为什么不工作。谢谢,我很高兴能帮上忙!我也犯过很多像这样愚蠢的错误:
/**
* 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);
}

}