Java 总变量赢得';t更新

Java 总变量赢得';t更新,java,eclipse,cumulative-sum,Java,Eclipse,Cumulative Sum,我是Java新手,所以我仍在努力理解如何正确地更新变量。我正在创建一个作为餐厅菜单的程序。列出的菜单项将出现在左侧,一旦用户检查一个项目并单击右侧按钮,该项目将出现在右侧,该项目的价格应添加到总变量中;然而,这是行不通的。有一些不必要的代码,但是如果可以的话,请忽略这些和一些注释。多谢各位 package finalproject; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class

我是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);

}
包最终项目;
导入java.awt.event.*;
导入java.awt.*;
导入javax.swing.*;
公共类MenuFrame扩展JFrame实现ActionListener{
最终双汉堡价格=6.24;
最终双鱼价格=13.78;
最终双汤价格=4.49;
最终双鸡价格=11.36;
最终双色拉价格=5.60;
最终双份薯条价格=3.24;
JPanel menuPanel=new JPanel();//用于放置菜单的面板,位于左侧
JPanel buttonPanel=new JPanel();//用于放置按钮的面板,位于中间
JPanel orderPanel=new JPanel();//放置订单菜单的面板,位于右侧
JLabel menuLabel=新的JLabel(“菜单”);//菜单的标签
JLabel orderLabel=new JLabel(“您的订单”);//用户订单的标签
JCheckBox-burger=新的JCheckBox(“汉堡:$6.24”,false);//汉堡复选框
JCheckBox fish=newjcheckbox(“fish:$13.78”,false);//fish复选框
JCheckBox-soup=newjcheckbox(“soup:$4.49”,false);//soup复选框
JCheckBox chicken=newjcheckbox(“chicken:$11.36”,false);//chicken复选框
JCheckBox沙拉=新的JCheckBox(“沙拉:$5.60”,false);//沙拉复选框
JCheckBox fries=新的JCheckBox(“fries:$3.24”,false);//fries复选框
JButton rightButton=newjbutton(“->”);//从左向右移动项目的按钮

JButton leftButton=new JButton(“totalPrice)”如果您不分配它,它不会被设置到文本字段中。 在刷新之前,请先执行以下操作:

totalLabel.setText("$" + totalPrice);

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

    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
    }
if(burger.isSelected()&&burger.getParent()==menuPanel){
//将汉堡从菜单移动到订单,取消选择汉堡,并将6.24添加到总价中
menuPanel.移除(汉堡);
orderPanel.add(汉堡);
汉堡包(假);
总价+=汉堡价格;

TotalAbel.setText(“$”+总价);告诉我们什么实际不起作用?不打印?无法设置变量?请提供更多详细信息1.如果您是Java新手,您可能不应该尝试编写GUI。2.您确实需要限制此处的代码量,以便只包含相关代码。此处的代码太多,而且涉及异步内容、侦听器等这确实增加了复杂性。请先在一些关键位置放置
System.out.println
,以确保您得到了应该添加到总数中的代码。因此,如果您认为应该选择汉堡,请在应该添加的
if
中放置
println
。如果您没有找到,请尝试其他方法
System.out.printlns
查看
source==rightbutton
是否为真,是否调用了
actionPerformed
等等。基本上,出错的方式太多了。你需要自己进行一些调试。一件明显奇怪的事情是:你所有的字段都是非静态的,除了你的总值之外。那就是我完全同意其他的评论:虽然你的技能处于如此基本的水平,但请不要去面对如此复杂的挑战。从小事做起,理解并掌握所有的基础知识;然后去做UI方面的事情。写下数百行复杂的代码……这让你难以理解nd…不是你应该扔给别人的东西。。。