无法从单选按钮向 ArrayList 正确添加值 - Java GUI [英] Cannot properly add values to ArrayList from radio Buttons - Java GUI

查看:52
本文介绍了无法从单选按钮向 ArrayList 正确添加值 - Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,允许用户选择餐厅菜单项并添加每个项目的总和,为每个人创建一个双餐总和.我还需要在每次输入一个新人时添加每顿饭(使用 Next Person 按钮)以得出我创建为 ArrayList 的 tableTotal.我似乎无法让数组列表添加的不仅仅是最后一个人的膳食总计.我需要它来包含每个人的膳食总和,即,如果有 4 个人,我的 tableTotal ArrayList 将包含 4 个唯一值.从这里开始,我将添加 tableTotal 中的所有值以得出整个表的双精度值.任何有关如何将每个人的 mealTotal 放入我的 tableTotal ArrayList 的帮助将不胜感激!

I have created a program that allows a user to choose restaurant menu items and adds the total of each item creating a double mealTotal for each person. I need to also add each mealTotal every time a new person is entered(using the Next Person button) to come up with the tableTotal which I have created as an ArrayList. I cannot seem to get the arraylist to add more than just the last person's mealTotal. I need it to include every person's mealTotal, i.e., if there were 4 people, my tableTotal ArrayList would hold 4 unique values. From here, I would add all of the values in tableTotal to come up with a double value for the entire table. Any help in how to put each person's mealTotal into my tableTotal ArrayList would be much appreciated!

这里是主类:

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

}

推荐答案

您的问题是您正在为每个人创建一个新的 MenuApp 对象,并且每个 MenuApp 对象都有自己的 tableTotal 字段.

Your problem is that you're creating a new MenuApp object for each person, and each MenuApp object has its own tableTotal field.

可能的解决方案:

  • 只为每个新人创建一个 MenuApp 对象并更改其状态,可能是 JLabel 上的文本.
  • 或者,如果您绝对必须为每个人创建一个新的 MenuApp,那么这样做,将它放在一个模态 JDialog 中,而不是一个 JFrame 中,当对话框完成时,调用代码查询它的总数,然后添加它到 tableTotal 字段.

这篇关于无法从单选按钮向 ArrayList 正确添加值 - Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆