不能正确地从单选按钮值添加到ArrayList中 - 的Java GUI [英] Cannot properly add values to ArrayList from radio Buttons - Java GUI

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

问题描述

我已经创建了一个程序,它允许用户选择餐馆的菜单项,并增加了总的每个项目创建为每个人双mealTotal的。我需要在每次进入一个新的人的时间(使用下一个人按钮)来与我曾作为一个ArrayList创建的tableTotal还添加每个mealTotal。我似乎无法获得ArrayList中添加比刚刚过去的人的mealTotal多。我需要它包括每个人的mealTotal,即,如果有4人,我tableTotal的ArrayList将举行4个独特的价值观。从这里,我想补充所有tableTotal值的拿出整个表的双重价值。在如何把每个人的mealTotal到我tableTotal ArrayList的任何帮助将是非常美联社preciated!

下面是主类:

 进口javax.swing.BoxLayout中;
进口javax.swing.JFrame中;公共类MenuApp扩展的JFrame {//静态的JFrame框架;公共静态无效的主要(字符串ARGS []){    JFrame的AFRAME =新MenuApp();
    aFrame.setVisible(真);}公共MenuApp(){    的setTitle(莉亚·汤普森的餐厅应用程序);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    按钮buttonPanel =新按钮();
    buttonPanel.setLayout(新的BoxLayout(buttonPanel,BoxLayout.Y_AXIS));    setContentPane(buttonPanel);
    包();
}
}

和按钮类:

 进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口javax.swing.ButtonGroup;
进口javax.swing.JButton中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JOptionPane中;
进口javax.swing.JPanel中;
进口javax.swing.JRadioButton;
进口javax.swing.JTextField中;
进口的java.util.ArrayList;公共类按钮继承JPanel {私人最终JTextField的名称字段;的String [] = drinkChoice {可乐,啤酒,酒};
的String [] = appChoice {汤,色拉,布里};
的String [] = entreeChoice {面食,牛排,汉堡};
的String [] = dessertChoice {提拉米苏,芝士蛋糕,巧克力蛋糕};一个JRadioButton [] = drinkBut​​tons新一个JRadioButton [drinkChoice.length]
一个JRadioButton [] = appButtons新一个JRadioButton [appChoice.length]
一个JRadioButton [] = entreeButtons新一个JRadioButton [entreeChoice.length]
一个JRadioButton [] = dessertButtons新一个JRadioButton [dessertChoice.length]ButtonGroup的buttonGroupDrink =新的ButtonGroup();
ButtonGroup的buttonGroupApp =新的ButtonGroup();
ButtonGroup的buttonGroupEntree =新的ButtonGroup();
ButtonGroup的buttonGroupDessert =新的ButtonGroup();JButton的mealCost;
JButton的nextPerson;
JButton的submitOrder;ArrayList的<双> tableTotal =新的ArrayList(0);双mealTotal = 0;
双mealTotal1 = 0;公共按钮(){    ActionListener的nameEntered =新NameEntered();
    添加(新的JLabel(名称(命中输入名称后输入)));
    名称字段=新的JTextField(10);
    添加(名称字段);
    nameField.addActionListener(nameEntered);    ActionListener的drinkSelection =新DrinkSelectionChangeMade();
    ActionListener的appSelection =新AppSelectionChangeMade();
    ActionListener的entreeSelection =新EntreeSelectionChangeMade();
    ActionListener的dessertSelection =新DessertSelectionChangeMade();    添加(新的JLabel(选择饮料));
    的for(int i = 0; I< drinkChoice.length;我++){
        drinkBut​​tons [I] =新一个JRadioButton(drinkChoice [I]);
        drinkBut​​tons [I] .getModel()setActionCommand(drinkChoice [I])。
        drinkBut​​tons [I] .addActionListener(drinkSelection);
        buttonGroupDrink.add(drinkBut​​tons [I]);
        添加(drinkBut​​tons [I]);
//如果(我== 0){
// mealTotal = mealTotal + 2;
//}否则如果(ⅰ== 1){
// mealTotal = mealTotal + 3;
//}其他{
// mealTotal = mealTotal + 5;
//}    }    添加(新的JLabel(选择开胃菜));
    的for(int i = 0; I< appChoice.length;我++){
        appButtons [I] =新一个JRadioButton(appChoice [I]);
        appButtons [I] .getModel()setActionCommand(appChoice [I])。
        appButtons [I] .addActionListener(appSelection);
        buttonGroupApp.add(appButtons [I]);
        添加(appButtons [I]);
//如果(我== 0){
// mealTotal = mealTotal + 6;
//}否则如果(ⅰ== 1){
// mealTotal = mealTotal + 7;
//}其他{
// mealTotal = mealTotal + 8;
//}    }    添加(新的JLabel(选择主菜));
    的for(int i = 0; I< entreeChoice.length;我++){
        entreeButtons [I] =新一个JRadioButton(entreeChoice [I]);
        entreeButtons [I] .getModel()setActionCommand(entreeChoice [I])。
        entreeButtons [I] .addActionListener(entreeSelection);
        buttonGroupEntree.add(entreeButtons [I]);
        添加(entreeButtons [I]);
//如果(我== 0){
// mealTotal = mealTotal + 12;
//}否则如果(ⅰ== 1){
// mealTotal = mealTotal + 20;
//}其他{
// mealTotal = mealTotal + 10;
//}    }    添加(新的JLabel(选择甜品));
    的for(int i = 0; I< dessertChoice.length;我++){
        dessertButtons [I] =新一个JRadioButton(dessertChoice [I]);
        dessertButtons [I] .getModel()setActionCommand(dessertChoice [I])。
        dessertButtons [I] .addActionListener(dessertSelection);
        buttonGroupDessert.add(dessertButtons [I]);
        添加(dessertButtons [I]);
//如果(我== 0){
// mealTotal = mealTotal + 7;
//}否则如果(ⅰ== 1){
// mealTotal = mealTotal + 6;
//}其他{
// mealTotal = mealTotal + 5;
//}    }
    mealCost =的新的JButton(膳食的总成本);
    加(mealCost);
    ActionListener的mealPrice =新MealCost pressed();
    mealCost.addActionListener(mealPrice);    nextPerson =的新的JButton(旁边的人);
    加(nextPerson);
    接下来ActionListener的=新NextPerson pressed();
    nextPerson.addActionListener(下);    submitOrder =的新的JButton(提交订单);
    加(submitOrder);
    ActionListener的提交=新提交pssed $ P $();
    submitOrder.addActionListener(提交);    //tableTotal.add(mealTotal);
    //添加(新的JLabel(您一共是:+ mealTotal));}私有类NameEntered实现的ActionListener {
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        字符串名称= nameField.getText();
        的System.out.println(名);
    }
} 私有类DrinkSelectionChangeMade实现的ActionListener {    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        字符串饮料= buttonGroupDrink.getSelection()getActionCommand()。
        的System.out.println(饮+作为饮料);
        如果(==饮可乐){
            mealTotal = mealTotal + 1.95;
        }
        否则,如果(饮==啤酒){
            mealTotal = mealTotal + 2.95;
        }
        其他{
            mealTotal = mealTotal + 4.95;
        }
    }
}    私有类AppSelectionChangeMade实现的ActionListener {    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        字符串app = buttonGroupApp.getSelection()getActionCommand()。
        的System.out.println(APP +作为开胃菜);
        如果(应用程序==汤){
            mealTotal = mealTotal + 3.95;
        }
        否则,如果(应用程序==沙拉){
            mealTotal = mealTotal + 4.95;
        }
        其他{
            mealTotal = mealTotal + 7.95;
        }
    }
}私有类EntreeSelectionChangeMade实现的ActionListener {    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        字符串主菜= buttonGroupEntree.getSelection()getActionCommand()。
        的System.out.println(主菜+作为主菜​​);
        如果(主菜==通心粉){
            mealTotal = mealTotal + 11.95;
        }
        否则,如果(==主菜牛排){
            mealTotal = mealTotal + 19.95;
        }
        其他{
            mealTotal = mealTotal + 9.95;
        }
    }
}私有类DessertSelectionChangeMade实现的ActionListener {    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
            字符串甜点= buttonGroupDessert.getSelection()getActionCommand()。
            的System.out.println(甜点+作为甜点);
            如果(甜点==提拉米苏){
            mealTotal = mealTotal + 6.95;
        }
        否则,如果(甜点==芝士蛋糕){
            mealTotal = mealTotal + 5.95;
        }
        其他{
            mealTotal = mealTotal + 4.95;
        }
            tableTotal.add(mealTotal);
    }
}
私有类NextPerson pressed实现的ActionListener {
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
      //移除所有();
      JFrame的nextFrame =新MenuApp();
      nextFrame.setVisible(真);
      的System.out.println(下一个顾客:);
    }
}私有类MealCost pressed实现的ActionListener {
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){        JOptionPane.showMessageDialog(mealCost,mealTotal);
    }
}私有类提交pressed实现的ActionListener {
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        JOptionPane.showMessageDialog(nextPerson,谢谢您的订单请立即退出。);
        的System.out.println(为表中的总成本是:+ tableTotal);
    }
}}


解决方案

您的问题是,你正在创建一个新MenuApp 每个人的对象,并且每个MenuApp对象都有自己的tableTotal领域。

可能的解决方案:


  • 创建只是一个MenuApp对象,并改变其状态,或许一个JLabel文字,每一个新的人。

  • 或者,如果你绝对要创建一个新的MenuApp每个人,那么这样做,将其放置在一个模式的JDialog,没有一个JFrame,并在对话框中完成对具有调用code查询它总与然后将其添加到tableTotal场。

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!

Here is the main class:

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


}
}

And the Buttons class:

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

}

解决方案

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

Possible solutions:

  • Create just one MenuApp object and change its state, perhaps the text on a JLabel, for each new person.
  • Or if you absolutely have to create a new MenuApp for each person, then do so, place it in a modal JDialog, not a JFrame, and when the dialog completes have the calling code query it for its total and then add it to a tableTotal field.

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

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