当我在 Eclipse 中运行我的程序时,它说有错误,但它们没有在左边距上标记,就像大多数错误都显示在该程序中一样。如果我绕过该消息,程序将正常运行。在 Eclipse 的控制台区域,它给了我这个:
javax.swing.jlabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0
这意味着什么?我该如何解决这个问题?您是否在程序中看到我可能想要更改的任何内容,即我应该删除/添加的内容,不需要的内容?
注意:更新的代码如下所示,更改了大小写,并按照以下建议添加了 getText()。
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class JPizzeria extends JFrame implements ActionListener, ItemListener {
DecimalFormat fmt = new DecimalFormat("0.00");
private double Price;
double smallPizza = 7;
double toppings;
JLabel title = new JLabel("Steve's Pizzeria");
Font headlineFont = new Font("Courier", Font.BOLD, 40);
JLabel info = new JLabel("What size of pizza do you wish to order?");
Font infoFont = new Font("Courier", Font.BOLD, 18);
JLabel info2 = new JLabel("What would you like on your pizza?");
Font info2Font = new Font("Courier", Font.BOLD, 18);
JLabel totalPrice = new JLabel("Total Price");
Font totalPriceFont = new Font("Courier", Font.BOLD, 18);
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);
JTextField totPrice = new JTextField(10);
public JPizzeria() {
String[] pizzaSize = { "Small-$7.00", "Medium-$9.00", "Large-$11.00",
"Extra-Large-$14.00" };
JComboBox decide = new JComboBox(pizzaSize);
add(title);
title.setFont(headlineFont);
add(info2);
info2.setFont(info2Font);
add(extraCheeseBox);
add(pepperoniBox);
add(sausageBox);
add(groundBeefBox);
add(onionBox);
add(mushroomBox);
add(blackOlivesBox);
add(greenPeppersBox);
add(info);
info.setFont(infoFont);
add(decide);
add(totalPrice);
totalPrice.setFont(totalPriceFont);
add(totPrice);
setLayout(new FlowLayout(FlowLayout.CENTER));
decide.setSelectedIndex(3);
decide.addActionListener(this);
totPrice.addActionListener(this);
totPrice.setText("$");
extraCheeseBox.addItemListener(this);
pepperoniBox.addItemListener(this);
sausageBox.addItemListener(this);
groundBeefBox.addItemListener(this);
onionBox.addItemListener(this);
mushroomBox.addItemListener(this);
blackOlivesBox.addItemListener(this);
greenPeppersBox.addItemListener(this);
}
public static void main(String[] args) {
JPizzeria aFrame = new JPizzeria();
final int WIDTH = 650;
final int HEIGHT = 250;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JComboBox decide = (JComboBox) e.getSource();
String pizzaSize = (String) decide.getSelectedItem();
System.out.println(pizzaSize);
if (pizzaSize.equals("Small-$7.00"))
Price = smallPizza + 0;
else if (pizzaSize.equals("Medium-$9.00"))
Price = smallPizza + 2;
else if (pizzaSize.equals("Large-$11.00"))
Price = smallPizza + 4;
else
Price = smallPizza + 7;
System.out.println(totalPrice.getText());;
totPrice.setText("$" + (fmt.format(Price + toppings)));
}
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));
}
}