Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - Trying to pass multiple values into a Label

I am trying to return the total meal price into the label totalOrderedLabel. The totalOrderedLabel label is returning 0.0 when I press the orderBtn. In totalOrderedLabel I want it to show the total Price of the meals selected.

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: " + descrip

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't comment that's why I'm writing in answer section

You are not calling the method updateModel() which adds the itmes to your OrderedFoodList. In addition, you are initializing the dLM in each method. You should explain your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...