总变量不会更新

时间:2016-11-24 19:53:36

标签: java eclipse running-total

我比Java更新,所以我仍然试图了解如何正确更新变量。我正在创建一个充当餐厅菜单的程序。列出的菜单项将显示在左侧,一旦用户检查项目并单击右键,项目将显示在右侧,并且项目的价格应该添加到总变量中;但是,这不起作用。有一些不必要的代码,但如果可以,请忽略这些和一些评论。谢谢。

package finalproject;

import java.awt.event.*;  
import java.awt.*;
import javax.swing.*;

public class MenuFrame extends JFrame implements ActionListener {

final double BURGER_PRICE = 6.24;
final double FISH_PRICE = 13.78;
final double SOUP_PRICE = 4.49;
final double CHICKEN_PRICE = 11.36;
final double SALAD_PRICE = 5.60;
final double FRIES_PRICE = 3.24;
JPanel menuPanel = new JPanel();                       //Panel to place the menu in, which will be on left side
JPanel buttonPanel = new JPanel();                     //Panel to place buttons in, which will be in center
JPanel orderPanel = new JPanel();                      //Panel to place order menu in, which will be on right side
JLabel menuLabel = new JLabel("Menu");                 //Label for the menu
JLabel orderLabel = new JLabel("Your Order");          //Label for the user's order
JCheckBox burger = new JCheckBox("Burger: $6.24", false);     //Burger check box
JCheckBox fish = new JCheckBox("Fish: $13.78", false);         //Fish check box
JCheckBox soup = new JCheckBox("Soup: $4.49", false);         //Soup check box
JCheckBox chicken = new JCheckBox("Chicken: $11.36", false);   //Chicken check box
JCheckBox salad = new JCheckBox("Salad: $5.60", false);       //Salad check box
JCheckBox fries = new JCheckBox("Fries: $3.24", false);       //Fries check box
JButton rightButton = new JButton("->");               //Button to move items from left to right
JButton leftButton = new JButton("<-");                //Button to move items from right to left
JLabel skipPosition = new JLabel("");                  //Label used to skip position in GridLayout
static double totalPrice = 0;                                        //Total price of order
Font titleFont = new Font("Times New Roman",Font.BOLD,20);
JLabel totalLabel = new JLabel("");  //total label

public MenuFrame() {

    setLayout(new GridLayout(0,3));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1055, 275);                                //Set size of Menu Frame

    menuPanel.setLayout(new GridLayout(0,3));         //Setting Menu's layout to GridLayout
    //Box buttonBox = Box.createVerticalBox();       //Creating box object for button panel
    buttonPanel.setLayout(new GridLayout(8,3));
    orderPanel.setLayout(new GridLayout(0,3));            //Setting Order's layout to FlowLayout

    menuLabel.setFont(titleFont);
    orderLabel.setFont(titleFont);

    //Adding menu items to Menu
    menuPanel.add(new JLabel(""));
    menuPanel.add(menuLabel);
    menuPanel.add(new JLabel(""));
    menuPanel.add(burger);
    menuPanel.add(fish);
    menuPanel.add(soup);
    menuPanel.add(chicken);
    menuPanel.add(salad);
    menuPanel.add(fries);

    //Adding buttons to Box
    //buttonBox.add(rightButton);
    //buttonBox.add(Box.createVerticalStrut(100));
    //buttonBox.add(leftButton);

    //Adding box to button panel
    //buttonPanel.add(buttonBox);

    //Adding "Your Order" label to Order. Items will be subsequently added or removed
    orderPanel.add(new JLabel(""));
    orderPanel.add(orderLabel);
    orderPanel.add(new JLabel(""));

    totalLabel.setText("$" + totalPrice);

    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(rightButton);
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(leftButton);
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(totalLabel);

    //Adding action listener to rightButton and leftButton
    rightButton.addActionListener(this);
    leftButton.addActionListener(this);

    add(menuPanel);
    add(buttonPanel);
    add(orderPanel);

}


@Override
public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();

    //If right button was clicked
    if(source == rightButton) {

        //if burger was selected and is in the menu panel
        if(burger.isSelected() && burger.getParent() == menuPanel) {

            //Move burger from menu to order, deselect burger, and add 6.24 to total price
            menuPanel.remove(burger);
            orderPanel.add(burger);
            burger.setSelected(false);
            totalPrice += BURGER_PRICE;

        }

        //if fish was selected and is in the menu panel
        if(fish.isSelected() && fish.getParent() == menuPanel) {

            //Move fish from menu to order, deselect fish, and add 13.78 to total price
            menuPanel.remove(fish);
            orderPanel.add(fish);
            fish.setSelected(false);
            totalPrice += FISH_PRICE;

        }

        //if soup was selected and is in the menu panel
        if(soup.isSelected() && soup.getParent() == menuPanel) {

            //Move soup from menu to order, deselect soup, and add 4.49 to total price
            menuPanel.remove(soup);
            orderPanel.add(soup);
            soup.setSelected(false);
            totalPrice += SOUP_PRICE;

        }

        //if chicken was selected and is in the menu panel
        if(chicken.isSelected() && chicken.getParent() == menuPanel) {

            //Move chicken from menu to order, deselect chicken, and add 11.36 to total price
            menuPanel.remove(chicken);
            orderPanel.add(chicken);
            chicken.setSelected(false);
            totalPrice += CHICKEN_PRICE;

        }

        //if salad was selected and is in the menu panel
        if(salad.isSelected() && salad.getParent() == menuPanel) {

            //Move salad from menu to order, deselect salad, and add 5.60 to total price
            menuPanel.remove(salad);
            orderPanel.add(salad);
            salad.setSelected(false);
            totalPrice += SALAD_PRICE;

        }

        //if fries was selected and is in the menu panel
        if(fries.isSelected() && fries.getParent() == menuPanel) {

            //Move fries from menu to order, deselect fries, and add 3.24 to total price
            menuPanel.remove(fries);
            orderPanel.add(fries);
            fries.setSelected(false);
            totalPrice += FRIES_PRICE;

        }

    }

    //if left button was clicked
    if(source == leftButton) {

        //if burger was selected and is in the order panel
        if(burger.isSelected() && burger.getParent() == orderPanel) {

            //Move burger from order to menu, deselect burger, and remove 6.24 from total price
            orderPanel.remove(burger);
            menuPanel.add(burger);
            burger.setSelected(false);
            totalPrice -= BURGER_PRICE;

        }

        //if fish was selected and is in the order panel
        if(fish.isSelected() && fish.getParent() == orderPanel) {

            //Move fish from order to menu, deselect fish, and remove 13.78 from total price
            orderPanel.remove(fish);
            menuPanel.add(fish);
            fish.setSelected(false);
            totalPrice -= FISH_PRICE;

        }

        //if soup was selected and is in the order panel
        if(soup.isSelected() && soup.getParent() == orderPanel) {

            //Move soup from order to menu, deselect soup, and remove 4.49 from total price
            orderPanel.remove(soup);
            menuPanel.add(soup);
            soup.setSelected(false);
            totalPrice -= SOUP_PRICE;

        }

        //if chicken was selected and is in the order panel
        if(chicken.isSelected() && chicken.getParent() == orderPanel) {

            //Move chicken from order to menu, deselect chicken, and remove 11.35 from total price
            orderPanel.remove(chicken);
            menuPanel.add(chicken);
            chicken.setSelected(false);
            totalPrice -= CHICKEN_PRICE;

        }

        //if salad was selected and is in the order panel
        if(salad.isSelected() && salad.getParent() == orderPanel) {

            //Move salad from order to menu, deselect salad, and remove 5.60 from total price
            orderPanel.remove(salad);
            menuPanel.add(salad);
            salad.setSelected(false);
            totalPrice -= SALAD_PRICE;

        }

        //if fries was selected and is in the order panel
        if(fries.isSelected() && fries.getParent() == orderPanel) {

            //Move fries from order to menu, deselect fries, and remove 3.24 from total price
            orderPanel.remove(fries);
            menuPanel.add(fries);
            fries.setSelected(false);
            totalPrice -= FRIES_PRICE;

        }

    }



    //Make changes appear
    repaint();            
    revalidate();        

}


public static void main(String[] args) {

    //Instantiating MenuFrame object
    MenuFrame menuFrame = new MenuFrame();


    //Making frame visible
    menuFrame.setVisible(true);

}

2 个答案:

答案 0 :(得分:0)

如果您不指定totalPrice,则不会将其设置到textField中。 在刷新之前把它放在:

totalLabel.setText("$" + totalPrice);

答案 1 :(得分:0)

变量更新正确,只是您没有重新设置totalLabel文本。

    if(burger.isSelected() && burger.getParent() == menuPanel) {

        //Move burger from menu to order, deselect burger, and add 6.24 to total price
        menuPanel.remove(burger);
        orderPanel.add(burger);
        burger.setSelected(false);
        totalPrice += BURGER_PRICE;
        totalLabel.setText("$" + totalPrice);  <<<<<< Add this to all of items
    }