2016-11-24 92 views
-2

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

} 
+1

告訴我們什麼實際上不工作?不打印?變量不能設置?請詳細說明 –

+2

1.如果您不熟悉Java,則可能不應該嘗試編寫GUI。 2.您確實需要限制代碼量,因此只包含相關的代碼。 – Carcigenicate

+1

這裏有太多的代碼,它涉及異步的東西和聽衆,以及真正增加複雜性的東西。請首先在一些關鍵位置放置'System.out.println',以確保您獲得了應該添加到總量中的代碼。所以如果你認爲應該選擇一個漢堡,那麼在應該添加的'if'裏面放一個'println'。如果你沒有到達那裏,那麼嘗試其他的'System.out.printlns'來查看'source == rightbutton'是否爲true,如果'actionPerformed'被調用,等等。 – ajb

回答

0

totalPrice不會被設置到textField中,如果您不指定它。 清爽之前將這個:

totalLabel.setText("$" + totalPrice); 
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 
    }