分配和if语句

时间:2012-10-08 02:30:12

标签: java

我不确定我的代码中的错误是什么,但订单变量似乎没有正确初始化,因此我的gui显示错误的输出,例如当选择奶酪,番茄和鸡肉时它只显示鸡肉和番茄番茄12.75美元。任何帮助将不胜感激:)

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

class PizzaFrame extends JFrame 
{
// The following components are defined (but not created).
private JButton newButton, exitButton;
private JLabel priceLabel, grandTotalLabel;
private JCheckBox chkCheese, chkChicken, chkTomato;
private JTextArea jTA;

// Other values used throughout this frame
private double total, orderTotal, grandTotal;
private String order = "";
private static final String noOrder =  "Base price of basic pizza is $10.00";

// set up GUI
public PizzaFrame() 
{
    // Ensure there is a suitable title for the frame
    setTitle("Welcome to Mylo's Pizza world, please make an order");
    setLayout(new FlowLayout());

    // Create the buttons for the user to click, and add them to the frame
    newButton = new JButton("New Order");
    exitButton = new JButton("Exit \t \n");
    add(newButton);
    add(exitButton);

    // Create the Check Boxes and add these to the frame.
    chkCheese = new JCheckBox("Cheese");
    chkChicken = new JCheckBox("Chicken");
    chkTomato = new JCheckBox("Tomato");
    add(chkCheese);
    add(chkChicken);
    add(chkTomato);


    priceLabel = new JLabel(noOrder);
    add(priceLabel);

    // Create the TextArea in which to display the progressive orders.
    jTA = new JTextArea(10,10);
    //JScrollPane scroller = new JScrollPane(jTA);
    //jTA.setLineWrap(true);
    //scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    //scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    add(jTA);
    //add(scroller);

    // Create the lower Label which displays the       
    grandTotalLabel = new JLabel("Startup - No orders taken yet. Total: $0");
    add(grandTotalLabel);


    ButtonHandler event_handler = new ButtonHandler();
    newButton.addActionListener(event_handler);
    exitButton.addActionListener(event_handler);


    // create and register listeners for the JCheckBoxes
    CheckBoxHandler box_listener = new CheckBoxHandler();
    chkCheese.addItemListener(box_listener);
    chkChicken.addItemListener(box_listener);
    chkTomato.addItemListener(box_listener);

    //set the size of the frame
    setSize( 250, 310 );

} 


private class ButtonHandler implements ActionListener 
{
    // start new order or exit on button event
    public void actionPerformed( ActionEvent event ) 
    {
        orderTotal = total;
        grandTotal += orderTotal;
        //boolean action = false;
        if (event.getSource() == newButton) 
        {
            // TO BE COMPLETED
            order = "";
            chkCheese.setSelected (false);
            chkChicken.setSelected(false);
            chkTomato.setSelected(false);
            //String order_total = Double.toString(orderTotal);
            jTA.append(order + " $" + Double.toString(orderTotal) + "\n");



        }
        else if (event.getSource() == exitButton) 
        {
            System.exit(0);
        }
    } 
} 



private class CheckBoxHandler implements ItemListener 
{
    static final double CHEESE_COST = 2.75;
    static final double CHICKEN_COST = 4.00;
    static final double TOMATO_COST = 0.50;

// respond to checkbox events by adding cost of pizza topping options to price
    public void itemStateChanged( ItemEvent event )
    {
            total = 10.00;
            // TO BE COMPLETED
            String selection = "";
            if (chkCheese.isSelected()) 
                total = total + CHEESE_COST;
            else if (chkChicken.isSelected()) 
                total = total + CHICKEN_COST;
            else if (chkTomato.isSelected()) 
                total = total + TOMATO_COST;



            if (chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected()) 
            {
                selection = "Cheese with Chicken and Tomato ";
                order += selection;
            }
            else if(chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected())
            {
                      //else
                          order += "Cheese with Chicken ";
            }
            else if(chkCheese.isSelected() && chkTomato.isSelected())
            {
                    //if (chkTomato.isSelected()) 
                        order += "Cheese with Tomato ";
            }

            else if (chkChicken.isSelected() && chkTomato.isSelected())
            {
                  //if (chkTomato.isSelected()) 
                      order += "Chicken with Tomato ";
            }
            else if(chkCheese.isSelected())
            {
                        order += "Cheese ";     
            }
            else if (chkChicken.isSelected())
            {
                      order += "Chicken ";
            }
            else if (chkTomato.isSelected())
            {
                      order += "Tomato ";
            }


        priceLabel.setText("Price of pizza is: $" +  Double.toString(total));
        grandTotalLabel.setText("Total income is: $" +  Double.toString(grandTotal));

    } 
} 

//----------------------------------------- Program Entry/Start point:

public static void main( String args[] ) 
{
    PizzaFrame pFrame = new PizzaFrame();
    pFrame.addWindowListener ( 

                                  // TO BE COMPLETED
            new WindowAdapter() 
            {   
                public void windowClosing (WindowEvent e) 
                {
                            System.exit(0); 
                }            
            }   

    );                          

    // Show the pizza frame ...
    pFrame.setVisible( true );
}

}

1 个答案:

答案 0 :(得分:3)

否则,如果退出,则满足条件。您希望价格是所有选定项目的总和,而不仅仅是第一个选定项目。 你的文本显示条件也有一个小错误。

public void itemStateChanged( ItemEvent event )
    {
            total = 10.00;
            // TO BE COMPLETED
            String selection = "";
            if (chkCheese.isSelected()) 
                total = total + CHEESE_COST;
            if (chkChicken.isSelected()) 
                total = total + CHICKEN_COST;
            if (chkTomato.isSelected()) 
                total = total + TOMATO_COST;



            if (chkCheese.isSelected() && chkChicken.isSelected() && chkTomato.isSelected()) 
            {
                selection = "Cheese with Chicken and Tomato ";
                order += selection;
            }
            else if(chkCheese.isSelected() && chkChicken.isSelected())
            {
                          order += "Cheese with Chicken ";
            }
            else if(chkCheese.isSelected() && chkTomato.isSelected())
            { 
                        order += "Cheese with Tomato ";
            }

            else if (chkChicken.isSelected() && chkTomato.isSelected())
            {
                      order += "Chicken with Tomato ";
            }
            else if(chkCheese.isSelected())
            {
                        order += "Cheese ";     
            }
            else if (chkChicken.isSelected())
            {
                      order += "Chicken ";
            }
            else if (chkTomato.isSelected())
            {
                      order += "Tomato ";
            }


        priceLabel.setText("Price of pizza is: $" +  Double.toString(total));
        grandTotalLabel.setText("Total income is: $" +  Double.toString(grandTotal));

    }