1

当我在 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));
    }
}
4

3 回答 3

1

如果有错误,但未在装订线中标记,则错误不在此文件中,而是在项目的其他位置,在另一个文件或项目配置中。您可以在问题视图中查看项目中的所有错误。如果您没有看到 Problems 视图,您可以通过转到Windows > Show View > Console或输入Alt+Shift+Q来显示它,然后输入X

在此处输入图像描述

您在控制台中获得的输出是由于这一行:

System.out.println(totalPrice);

totalPrice是 aJLabel所以它会打印关于组件本身的信息,比如它的位置和其他属性。这与表明您的项目中有错误的消息无关。

如果您想要打印 JLabel 的文本,则应将其替换为:

System.out.println(totalPrice.getText());

建议

当您寻求建议时,您应该做的第一件事是不要将您的类设置为 JFrame 的子类。这是不必要的,也不灵活。只需创建一个 JFrame 对象并在其中添加您的东西。

此外,以大写字母开头的变量或字段名称是不好的 Java 风格,就像您对所有 JCheckBox 实例所做的那样。看看这如何混淆 StackOverflow 的语法高亮显示。第一个大写字母的名称用于类、接口或枚举。

最后,您似乎有一个错误:如果您先选择比萨大小,然后再选择浇头,则总价格将仅是浇头的总价格,而不是比萨饼本身的价格。

于 2013-11-06T18:14:44.987 回答
0

When I run my program in Eclipse, it says there are errors but they are not marked on the left margin like most of the errors are shown in that program

You are not making any syntactical errors at all. Why would it show you an error when you haven't made any ?


What does that mean?

You are printing out your JLabel.
What you see is not an error, rather it is the String representation of your JLabel.


How can I go about fixing this problem? Do you see anything in the program that I might want to change, i.e. things I should remove/add, things that are not needed?

If you would like to get the text that is in the JLabel, use the getText() method of JLabel.
You would do it as shown below:

System.out.println(totalPrice.getText());  
于 2013-11-06T18:17:48.767 回答
0

在这个方法中

    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);
    totPrice.setText("$" + (fmt.format(Price + toppings)));
}

你在说System.out.println(totalPrice)。所以你实际上是在打印文本框,而不是文本框中的数据。为此,请使用System.out.println(totalPrice.getText());

于 2013-11-06T18:27:19.567 回答