我决定完全废弃我的上一个项目并重做它。这一次我的问题是我无法正确加价。您是否介意查看我的代码并告诉我需要做些什么来解决它?
提前谢谢!
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.event.*;
import java.text.DecimalFormat;
class JFrameWithComponents extends JFrame implements ItemListener {
DecimalFormat fmt = new DecimalFormat("0.00");
final int BASE_PRICE = 7;
int totalPrice = BASE_PRICE;
int toppings = 0;
String output;
JLabel title = new JLabel("Steve's Pizzeria");
Font headlineFont = new Font("Courier", Font.BOLD, 40);
JLabel info = new JLabel("What would you like on your pizza?");
Font infoFont = new Font("Courier", Font.BOLD, 18);
JLabel info2 = new JLabel("What size of pizza do you wish to order?");
Font info2Font = new Font("Courier", Font.BOLD, 18);
JLabel info3 = new JLabel("Your total is: ");
Font info3Font = new Font("Courier", Font.BOLD, 24);
JCheckBox extraCheeseBox = new JCheckBox("Extra Cheese", false);
JCheckBox pepperoniBox = new JCheckBox("Pepperoni", false);
JCheckBox sausageBox = new JCheckBox("Sausage", false);
JCheckBox groundBeefBox = new JCheckBox("Ground Beef", false);
JCheckBox onionBox = new JCheckBox("Onions", false);
JCheckBox mushroomBox = new JCheckBox("Mushrooms", false);
JCheckBox blackOlivesBox = new JCheckBox("Black Olives", false);
JCheckBox greenPeppersBox = new JCheckBox("Green Peppers", false);
JCheckBox jalepenosBox = new JCheckBox("Jalepenos", false);
JTextField totPrice = new JTextField(10);
JComboBox pizzaBox = new JComboBox();
final int WIDTH = 650;
final int HEIGHT = 250;
int[] pizzaPrice = { 7, 9, 11, 14 };
public JFrameWithComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(WIDTH, HEIGHT);
add(title);
title.setFont(headlineFont);
add(info2);
info2.setFont(info2Font);
add(pizzaBox);
add(info);
add(extraCheeseBox);
add(pepperoniBox);
add(sausageBox);
add(groundBeefBox);
add(onionBox);
add(mushroomBox);
add(blackOlivesBox);
add(greenPeppersBox);
add(jalepenosBox);
add(info3);
info3.setFont(info3Font);
add(totPrice);
pizzaBox.addItem("small ($7)");
pizzaBox.addItem("medium ($9)");
pizzaBox.addItem("large ($11)");
pizzaBox.addItem("extra large ($14)");
pizzaBox.addItemListener(this);
extraCheeseBox.addItemListener(this);
pepperoniBox.addItemListener(this);
sausageBox.addItemListener(this);
groundBeefBox.addItemListener(this);
onionBox.addItemListener(this);
mushroomBox.addItemListener(this);
blackOlivesBox.addItemListener(this);
greenPeppersBox.addItemListener(this);
jalepenosBox.addItemListener(this);
}
public void itemStateChanged(ItemEvent event) {
Object source = event.getSource();
int select = event.getStateChange();
if (source == extraCheeseBox) {
if (select == ItemEvent.SELECTED) {
toppings += 1;
} else
toppings -= 1;
} else if (source == pepperoniBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == sausageBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == groundBeefBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == onionBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == mushroomBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == blackOlivesBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (source == greenPeppersBox) {
if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
} else if (select == ItemEvent.SELECTED)
toppings += 1.00;
else
toppings -= 1.00;
totPrice.setText("$ " + fmt.format(toppings) + pizzaPrice);
output = "$ " + (totalPrice + toppings);
totPrice.setText(output);
}
}
public class JPizza {
public static void main(String[] args) {
JFrameWithComponents frame = new JFrameWithComponents();
frame.setVisible(true);
}
}
// end of class
答案 0 :(得分:0)
根据您的代码,您只需从头开始重新计算价格,不要添加和减去值,因为它很容易让状态出错。
另外,你不包括披萨的价格。
更好的解决方案是将总计算与事件完全分开......
例如......
@Override
public void itemStateChanged(ItemEvent event) {
double total = getTotal();
totPrice.setText(NumberFormat.getCurrencyInstance().format(total));
}
protected double getTotal() {
double total = 0;
total = pizzaPrice[pizzaBox.getSelectedIndex()];
total += extraCheeseBox.isSelected() ? 1 : 0;
total += pepperoniBox.isSelected() ? 1 : 0;
total += sausageBox.isSelected() ? 1 : 0;
total += groundBeefBox.isSelected() ? 1 : 0;
total += onionBox.isSelected() ? 1 : 0;
total += mushroomBox.isSelected() ? 1 : 0;
total += blackOlivesBox.isSelected() ? 1 : 0;
total += greenPeppersBox.isSelected() ? 1 : 0;
total += jalepenosBox.isSelected() ? 1 : 0;
return total;
}