我试图将总餐价退回到标签totalOrderedLabel。当我按orderBtn时,totalOrderedLabel标签返回0.0。在totalOrderedLabel中,我希望它显示所选餐点的总价格。
ArrayList<Food> OrderedFoodList = new ArrayList<>();
/**
* Launch the application.
*/
public static void NewScreen() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MenuPage frame = new MenuPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MenuPage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 845, 363);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// StartPage start = new StartPage(MenuPage.this);
// start.setVisible(true);
// start.setModal(true);
menuTxt = new JTextField();
menuTxt.setBounds(295, 1, 81, 26);
menuTxt.setHorizontalAlignment(SwingConstants.CENTER);
menuTxt.setEditable(false);
menuTxt.setText("Menu");
contentPane.add(menuTxt);
menuTxt.setColumns(10);
mealSelectTxt = new JTextField();
mealSelectTxt.setHorizontalAlignment(SwingConstants.CENTER);
mealSelectTxt.setEditable(false);
mealSelectTxt.setText("Please select a meal");
mealSelectTxt.setBounds(253, 39, 154, 26);
contentPane.add(mealSelectTxt);
mealSelectTxt.setColumns(10);
txtOrder = new JTextField();
txtOrder.setHorizontalAlignment(SwingConstants.CENTER);
txtOrder.setEditable(false);
txtOrder.setText("Order");
txtOrder.setBounds(520, 39, 91, 26);
contentPane.add(txtOrder);
txtOrder.setColumns(10);
dLM=new menulist();
mealList = new JList();
mealList.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
ListModel mealListModel = new DefaultComboBoxModel
(new String[] { "Select an item from the menu." });
mealList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
menulist model = (menulist) mealList.getModel();
updateLabel(model);
}
}
});
mealList.setModel(mealListModel);
mealList.setBounds(206, 72, 220, 105);
contentPane.add(mealList);
starterBtn = new JButton("Starter");
starterBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menulist dLM=new menulist();
dLM.addItem("Tomato Soup", "Creamy Tomato Soup served with a warm crusty bread roll.", "235", 3.90);
dLM.addItem("Garlic & Cheese Mushrooms (V)", "Baked mushrooms in a garlic and cheese sauce.", "175", 5.60);
dLM.addItem("Garlic Bread & Cheese Pizza Bread", "Garlic pizza bread with mozzarella cheese.", "480", 5.50);
dLM.addItem("Bruschetta Napoli (V)", "Crisp ciabatta bread topped with homemade bruschetta.", "430", 4.80);
}
});
mainBtn = new JButton("Main");
mainBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menulist dLN=new menulist();
dLN.addItem("Chicken & Chips", "Chicken breast served with a side of chips and vegetables.", "560", 8.30);
dLN.addItem("Steak","Filet steak served with saute potatoes and mushrooms.","670", 10.90);
dLN.addItem("American Hot Pizza","Pepperoni, fresh green chilli and mozzarella, on our classic thin crust base.","520", 9.20);
dLN.addItem("BBQ Cheese Burger","Our classic 100% Irish beef burger, topped with cheese and our signature BBQ sauce, served in a soft Sicilian style bun with baby gem lettuce.","600", 8.50);
dLN.addItem("Chicken Penne Bake","Penne pasta with chicken, pepperoni, streaky bacon, rocket and Neapolitan sauce, topped with a cheese crumb.", "800", 8.20);
dLN.addItem("Veggie Burger (V)","A Southern-Italian style mix of red peppers, olives, sundried tomatoes and mozzarella, served in a soft Sicilian style bun with baby gem lettuce, tomato and mayo. Served with golden fries and chilli relish. ", "630", 8.00);
mealList.setModel(dLN);
}
});
dessertBtn = new JButton("Dessert ");
dessertBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menulist dLB=new menulist();
dLB.addItem("Dairy Ice Cream", "Three scoops of real dairy ice cream with a fan wafer. Choose from: Vanilla, Strawberry, Chocolate, Toffee, Mint Choc Chip, Brownies & Cream or Blueberry.", "440", 4.80);
dLB.addItem("Vanilla Cheesecake", "The richest, creamiest and most delicious cheesecake you’ll ever taste, topped with a red berry sauce and served with a scoop of real dairy vanilla ice cream.", "390", 5.10);
dLB.addItem("Brownies & Ice Cream", "Warm chocolate brownies with cream, brownies & cream ice cream and chocolate flavour sauce.", "520", 4.90);
dLB.addItem("Cinnamon Waffle Crunch", "A warm cinnamon waffle topped with vanilla ice cream, toffee crunch pieces and butterscotch flavour sauce.", "360", 4.60);
mealList.setModel(dLB);
}
});
JButton drinksBtn = new JButton("Drinks");
drinksBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menulist dLD=new menulist();
dLD.addItem("Diet Coke", "", "1", 1.60);
dLD.addItem("Coca Cola", "", "130", 1.60);
dLD.addItem("Orange Juice", "", "80", 1.30);
dLD.addItem("Still Water", "", "0", 1.50);
dLD.addItem("Sparkling Water", "", "0", 1.60);
dLD.addItem("Stella Artois", "", "180", 2.20);
dLD.addItem("Red Wine", "", "160", 2.60);
dLD.addItem("White Wine", "", "180", 2.60);
mealList.setModel(dLD);
}
});
vegBtn = new JButton("Vegetarian (V)");
vegBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menulist dLV=new menulist();
dLV.addItem("Mozerella Dippers (V)", "Crispy breaded Mozzarella with tomato & basil sauce.", "300", 5.30);
dLV.addItem("BBQ Houmous (V)", "Houmous drizzled with BBQ sauce, served with warm garlic pita and tortilla chips.", "330", 5.10);
dLV.addItem("Double Layered Nachos (V)", "Tortilla chips, melted cheese. Served with spicy guacamole.", "630", 7.20);
dLV.addItem("Loaded Potato Skins (V)", "Potato skins filled with melted cheese and vegetables", "360", 6.50);
dLV.addItem("Four Cheese Pizza (V)", "Goat’s cheese, cheddar, smoked cheese, mozzarella, red onion and fresh thyme, on our classic thin crust base.", "560", 7.60);
dLV.addItem("Chocolate Fudge (V)", "Three layers of chocolate cake with rich chocolate & fudge filling, served warm with real dairy ice cream.", "450", 4.40);
mealList.setModel(dLV);
}
});
vegBtn.setBounds(10, 173, 117, 29);
contentPane.add(vegBtn);
drinksBtn.setBounds(98, 65, 75, 29);
contentPane.add(drinksBtn);
dessertBtn.setBounds(10, 111, 81, 29);
contentPane.add(dessertBtn);
mainBtn.setBounds(9, 72, 81, 29);
contentPane.add(mainBtn);
starterBtn.setBounds(10, 39, 81, 29);
contentPane.add(starterBtn);
menulist listOrder = new menulist();
orderList = new JList(listOrder);
orderList.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
orderList.setBounds(457, 72, 241, 105);
contentPane.add(orderList);
JButton orderBtn = new JButton();
orderBtn.setText("Place Order");
orderBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectionNumber = mealList.getSelectedIndex();
if (selectionNumber == -1){
JOptionPane.showMessageDialog(MenuPage.this, "Please select a meal");
} else {
Food orderedMeal = (Food) mealList.getSelectedValue();
JOptionPane.showMessageDialog(MenuPage.this, "You have ordered " //clear the last order . find a code to do that
+ orderedMeal + "");
listOrder.addElement(orderedMeal);
double totalPrice = 0;
for(Food food : OrderedFoodList) {
totalPrice = totalPrice + food.getPrice();
}
totalOrderedLabel.setText("Total price: " + totalPrice);
//orderList.clear();
}
}
});
orderBtn.setBounds(259, 189, 117, 29);
contentPane.add(orderBtn);
reviewBtn = new JButton("Review Order");
reviewBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ReviewOrder nw = new ReviewOrder(); // the new visual class window has been created and called MenuPage. The proceedMenuBtn
nw.NewScreen();
}
});
reviewBtn.setBounds(574, 263, 117, 29);
contentPane.add(reviewBtn);
priceLabel = new JLabel("Price =£0.00");
priceLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
priceLabel.setBounds(82, 258, 91, 16);
contentPane.add(priceLabel);
descLabel = new JLabel("Description: pick an item from the list ");
descLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
descLabel.setBounds(166, 258, 396, 16);
contentPane.add(descLabel);
kcalLabel = new JLabel("kcal = 0");
kcalLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
kcalLabel.setBounds(9, 258, 61, 16);
contentPane.add(kcalLabel);
tableLabel = new JLabel("Table : " + Restaurant.tableNumber);
tableLabel.setBounds(16, 6, 91, 16);
contentPane.add(tableLabel);
JLabel dinersLabel = new JLabel("Diners : " + Restaurant.dinerNumber);
dinersLabel.setBounds(119, 6, 81, 16);
contentPane.add(dinersLabel);
JButton removeButton = new JButton("remove");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectionNumber = orderList.getSelectedIndex();
if (selectionNumber == -1) {
JOptionPane.showMessageDialog(MenuPage.this, "Remove Item");
} else {
listOrder.removeElementAt(selectionNumber);
}
}
});
removeButton.setBounds(585, 189, 117, 29);
contentPane.add(removeButton);
totalOrderedLabel = new JLabel("Total Price");
totalOrderedLabel.setBounds(436, 202, 126, 16);
contentPane.add(totalOrderedLabel);
}
public void updateLabel(menulist model) {
int selectionNumber = mealList.getSelectedIndex();
if (selectionNumber == -1){
//JOptionPane.showMessageDialog(MenuPage.this, "Please select a meal");
} else {
Food menulist = (Food) mealList.getSelectedValue();
Food itemFood = (Food) ((menulist) model).getElementAt(selectionNumber);
String description = itemFood.getDescription(); // could use this to set table label value.
double price = itemFood.getPrice();
String calories = itemFood.getKCal();
priceLabel.setText("Price = £" + price);
descLabel.setText("Description: " + description);
kcalLabel.setText("Kcal = " + calories);
}
}
public void updateModel(){
int selectionNumber = mealList.getSelectedIndex();
Food itemFood = (Food) ((menulist) dLM).getElementAt(selectionNumber);
//Add new food to current order
OrderedFoodList.add(itemFood);
}
}
答案 0 :(得分:2)
我无法评论为什么我在回答部分写作
您没有调用 updateModel()方法,该方法会将该主题添加到OrderedFoodList
。此外,您正在初始化每种方法中的dLM。你应该解释一下你的代码。