2014-02-10 26 views
1

我已經創建了一個程序,允許用戶選擇餐館菜單項並添加每個項目的總數,爲每個人創建一個雙重菜單項。每次輸入新用戶時(使用Next Person按鈕),我還需要添加每個mealTotal以創建我作爲ArrayList創建的tableTotal。我似乎無法讓陣列列表添加更多最後一個人的mealTotal。我需要它包含每個人的mealTotal,即如果有4個人,我的tableTotal ArrayList將包含4個唯一值。從這裏開始,我會將tableTotal中的所有值添加到整個表的double值中。任何幫助如何把每個人的mealTotal到我的tableTotal ArrayList將不勝感激!無法從單選按鈕向ArrayList正確添加值 - Java GUI

這裏是主類:

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

} 
+0

爲什麼要爲每個新人創建一個新的JFrame?爲什麼要爲每個新人創建一個新的MenuApp?爲什麼不相反只是顯示*一個* JFrame,並更改JLabel的文本,告訴你哪個人當前正在訂購。 –

回答

3

你的問題是,你要爲每個人創建一個新MenuApp對象,每個MenuApp對象有自己的tableTotal字段。

可能的解決方案:

  • 只創建一個MenuApp對象,並改變其狀態,也許是一個JLabel的文本,爲每一個新的人。
  • 或者,如果您絕對必須爲每個人創建一個新的MenuApp,那麼請將其置於一個模態JDialog中,而不是JFrame中,並且當對話框完成時,調用代碼會查詢它的總數,然後添加它到tableTotal字段。
+0

謝謝,解決了這個問題!我只是爲輸入的每個新人清除了按鈕和文本框,而不是創建新的JFrame/MenuApp。 – lchristina26

+0

@ lchristina26:很高興你有它的工作! –