2016-11-24 4 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'が真であるかどうか、 '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 
    } 
関連する問題