I am not to sure what the error is in my code but the order variable does not seem to initialise properly and hence my gui displays the wrong output, for example when cheese,tomato and chicken are selected it only displays Chicken with Tomato Tomato $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 );
}
}