Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_User Interface_Radio Button_Jframe - Fatal编程技术网

创建一个菜单应用程序,显示膳食价格总计,增加Java中的单选按钮值

创建一个菜单应用程序,显示膳食价格总计,增加Java中的单选按钮值,java,user-interface,radio-button,jframe,Java,User Interface,Radio Button,Jframe,我的任务是创建一个餐厅菜单应用程序,允许用户选择饮料、开胃菜、主菜和甜点,每个都有三个选项。我必须使用单选按钮。每次按下nextPerson按钮时,我应该增加每个人的mealTotal,mealTotal被添加到下一个人的总计中,以打印整个表的总计。如果我能为每个人提供适当的mealTotal增量,然后获得tableTotal,我将不胜感激 以下是主要课程: import javax.swing.BoxLayout; import javax.swing.JFrame; public clas

我的任务是创建一个餐厅菜单应用程序,允许用户选择饮料、开胃菜、主菜和甜点,每个都有三个选项。我必须使用单选按钮。每次按下nextPerson按钮时,我应该增加每个人的mealTotal,mealTotal被添加到下一个人的总计中,以打印整个表的总计。如果我能为每个人提供适当的mealTotal增量,然后获得tableTotal,我将不胜感激

以下是主要课程:

import javax.swing.BoxLayout;
import javax.swing.JFrame;

public class MenuApp extends JFrame {

//    static JFrame frame;

public static void main(String args[]) {

    JFrame aFrame = new MenuApp();
    aFrame.setVisible(true);

}

public MenuApp() {

    setTitle("Leah Thompson's Restaurant App");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Buttons buttonPanel = new Buttons();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));

    setContentPane(buttonPanel);
    pack();


}
}
按钮类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.util.ArrayList;

public class Buttons extends JPanel {

private final JTextField nameField;

String[] drinkChoice = {"Coke", "Beer", "Wine"};
String[] appChoice = {"Soup", "Salad", "Brie"};
String[] entreeChoice = {"Pasta", "Steak", "Burger"};
String[] dessertChoice = {"Tiramisu", "Cheesecake", "Chocolate Cake"};

JRadioButton[] drinkButtons = new JRadioButton[drinkChoice.length];
JRadioButton[] appButtons = new JRadioButton[appChoice.length];
JRadioButton[] entreeButtons = new JRadioButton[entreeChoice.length];
JRadioButton[] dessertButtons = new JRadioButton[dessertChoice.length];

ButtonGroup buttonGroupDrink = new ButtonGroup();
ButtonGroup buttonGroupApp = new ButtonGroup();
ButtonGroup buttonGroupEntree = new ButtonGroup();
ButtonGroup buttonGroupDessert = new ButtonGroup();

JButton mealCost;
JButton nextPerson;
JButton submitOrder;

ArrayList<Double> tableTotal = new ArrayList(0);

double mealTotal = 0;
double mealTotal1 = 0;

public Buttons(){

    ActionListener nameEntered = new NameEntered(); 
    add(new JLabel("Name (hit enter after typing name)"));
    nameField = new JTextField(10);
    add(nameField);
    nameField.addActionListener(nameEntered);

    ActionListener drinkSelection = new DrinkSelectionChangeMade();
    ActionListener appSelection = new AppSelectionChangeMade();
    ActionListener entreeSelection = new EntreeSelectionChangeMade();
    ActionListener dessertSelection = new DessertSelectionChangeMade();

    add(new JLabel("Choose a Drink"));
    for (int i = 0; i < drinkChoice.length; i++) {
        drinkButtons[i] = new JRadioButton(drinkChoice[i]);
        drinkButtons[i].getModel().setActionCommand(drinkChoice[i]);
        drinkButtons[i].addActionListener(drinkSelection);
        buttonGroupDrink.add(drinkButtons[i]);
        add(drinkButtons[i]);
//            if (i == 0) {
//                mealTotal = mealTotal + 2;
//            } else if (i == 1) {
//                mealTotal = mealTotal + 3;
//            } else {
//                mealTotal = mealTotal + 5;
//            }

    }

    add(new JLabel("Choose an Appetizer"));
    for (int i = 0; i < appChoice.length; i++) {
        appButtons[i] = new JRadioButton(appChoice[i]);
        appButtons[i].getModel().setActionCommand(appChoice[i]);
        appButtons[i].addActionListener(appSelection);
        buttonGroupApp.add(appButtons[i]);
        add(appButtons[i]);
//            if (i == 0) {
//                mealTotal = mealTotal + 6;
//            } else if (i == 1) {
//                mealTotal = mealTotal + 7;
//            } else {
//                mealTotal = mealTotal + 8;
//            }

    }

    add(new JLabel("Choose an Entree"));
    for (int i = 0; i < entreeChoice.length; i++) {
        entreeButtons[i] = new JRadioButton(entreeChoice[i]);
        entreeButtons[i].getModel().setActionCommand(entreeChoice[i]);
        entreeButtons[i].addActionListener(entreeSelection);
        buttonGroupEntree.add(entreeButtons[i]);
        add(entreeButtons[i]);
//            if (i == 0) {
//                mealTotal = mealTotal + 12;
//            } else if (i == 1) {
//                mealTotal = mealTotal + 20;
//            } else {
//                mealTotal = mealTotal + 10;
//            }

    }

    add(new JLabel("Choose a Dessert"));
    for (int i = 0; i < dessertChoice.length; i++) {
        dessertButtons[i] = new JRadioButton(dessertChoice[i]);
        dessertButtons[i].getModel().setActionCommand(dessertChoice[i]);
        dessertButtons[i].addActionListener(dessertSelection);
        buttonGroupDessert.add(dessertButtons[i]);
        add(dessertButtons[i]);
//            if (i == 0) {
//                mealTotal = mealTotal + 7;
//            } else if (i == 1) {
//                mealTotal = mealTotal + 6;
//            } else {
//                mealTotal = mealTotal + 5;
//            }

    }


    mealCost = new JButton("Total Cost of Meal");
    add(mealCost);
    ActionListener mealPrice = new MealCostPressed();
    mealCost.addActionListener(mealPrice);

    nextPerson = new JButton("Next Person");
    add(nextPerson);
    ActionListener next = new NextPersonPressed();
    nextPerson.addActionListener(next);

    submitOrder = new JButton("Submit Order");
    add(submitOrder);
    ActionListener submit = new SubmitPressed();
    submitOrder.addActionListener(submit);

    //tableTotal.add(mealTotal);
    //add(new JLabel("Your total is: " + mealTotal));

}

private class NameEntered implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        String name = nameField.getText();
        System.out.println(name);
    }
}

 private class DrinkSelectionChangeMade implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String drink = buttonGroupDrink.getSelection().getActionCommand();
        System.out.println(drink + " as a drink");
        if(drink == "Coke"){
            mealTotal = mealTotal + 1.95;
        }
        else if(drink == "Beer"){
            mealTotal = mealTotal + 2.95;
        }
        else{
            mealTotal = mealTotal + 4.95;
        }
    }
}

    private class AppSelectionChangeMade implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String app = buttonGroupApp.getSelection().getActionCommand();
        System.out.println(app + " as an appetizer");
        if(app == "Soup"){
            mealTotal = mealTotal + 3.95;
        }
        else if(app == "Salad"){
            mealTotal = mealTotal + 4.95;
        }
        else{
            mealTotal = mealTotal + 7.95;
        }
    }
}

private class EntreeSelectionChangeMade implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String entree = buttonGroupEntree.getSelection().getActionCommand();
        System.out.println(entree + " as an entree");
        if(entree == "Pasta"){
            mealTotal = mealTotal + 11.95;
        }
        else if(entree == "Steak"){
            mealTotal = mealTotal + 19.95;
        }
        else{
            mealTotal = mealTotal + 9.95;
        }
    }
}

private class DessertSelectionChangeMade implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
            String dessert = buttonGroupDessert.getSelection().getActionCommand();
            System.out.println(dessert + " as a dessert");
            if(dessert == "Tiramisu"){
            mealTotal = mealTotal + 6.95;
        }
        else if(dessert == "Cheesecake"){
            mealTotal = mealTotal + 5.95;
        }
        else{
            mealTotal = mealTotal + 4.95;
        }
            tableTotal.add(mealTotal);
    }
}
private class NextPersonPressed implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
      //removeAll();
      JFrame nextFrame = new MenuApp();
      nextFrame.setVisible(true);
      System.out.println("Next Customer: "); 
    }
}

private class MealCostPressed implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){

        JOptionPane.showMessageDialog(mealCost, mealTotal);
    }
}

private class SubmitPressed implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        JOptionPane.showMessageDialog(nextPerson, "Thank you for your order. Please Exit Now.");
        System.out.println("The total cost for the table is: " + tableTotal);
    }
}

}

你需要一些东西

要显示计数的某个组件,声明为实例变量 一个tally变量,用于维护声明为double的tally,是的,您可以使用long,但我不想让您感到困惑。 确定所选/未选项目值的某种方法。 调用某个ActionListener时,需要确定所选项目的值,然后需要确定是否选择了源

如果选择了该项目,则需要将其值添加到计数中;如果未选择该项目,则需要减去该值


然后,您需要设置计数组件的值,可能需要使用类似NumberFormat.getCurrencyInstance的东西。我更改了代码,以便它可以为一个人获取mealTotal,您对如何为每个新输入的人添加mealTotal有何建议?例如,每次按下NextPerson按钮时,前一个人的餐前小吃添加到新人的餐前小吃中,最后打印一张双人桌合计?谢谢那要看情况而定,你是想保留前一个人的每一件物品,还是只保留记录。如果要维护每个人的订单,请创建一个POJO作为值的容器并将它们添加到列表中。我刚刚添加了一个arrayList、tableTotal,并尝试将mealTotal添加到列表中,但它只添加最后一个人的mealTotal,因此其中只有一个值。我无法添加每个人的总餐数并将其全部存储在tableTotal中…听起来您有一个参考问题,但如果没有一些代码,很难知道。也许你应该考虑提出另一个问题,谢谢,我只是用一个新的代码添加了一个新问题。