简体   繁体   English

javax.swing.jlabel错误

[英]javax.swing.jlabel Error

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. 当我在Eclipse中运行程序时,它说有错误,但是没有像大多数错误在该程序中所示的那样在左空白处标记。 The program runs correctly if I bypass that message. 如果我绕过该消息,程序将正确运行。 In the console area of Eclipse, it gives me this: 在Eclipse的控制台区域中,它给出了以下信息:

javax.swing.jlabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0

What does that mean? 这意味着什么? How can I go about fixing this problem? 我该如何解决这个问题? Do you see anything in the program that I might want to change, ie things I should remove/add, things that are not needed? 您在程序中是否看到我可能想要更改的任何内容,即应该删除/添加的内容,不需要的内容?

NOTE: Updated code is below with the case change and the getText() added as suggested below. 注意:下面是更新的代码,带有大小写更改,并按以下建议添加了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));
    }
}

If there are errors, but they are not marked in the gutter, then the error is not in this file, but somewhere else in your project, either in another file or in the project configuration. 如果存在错误,但未在装订线中标记错误,则该错误不在此文件中,而是在项目的其他位置(在另一个文件中或在项目配置中)。 You can see all errors in your project in the Problems view. 您可以在“ 问题”视图中查看项目中的所有错误。 If you do not see the Problems view, you can show it by going to Windows > Show View > Console or typing Alt+Shift+Q , then X . 如果看不到“问题”视图,则可以通过以下方法显示它:转到Windows>显示视图>控制台,或依次输入Alt + Shift + QX。

在此处输入图片说明

The output that you obtain in the console is due to this line: 您在控制台中获得的输出归因于以下行:

System.out.println(totalPrice);

totalPrice is a JLabel so it will print information about the component itself, like its position and other properties. totalPrice是一个JLabel因此它将打印有关组件本身的信息,例如其位置和其他属性。 This is not related to the message that states you have errors in your project. 这与表明您的项目中有错误的消息无关。

If what you want is to print the text of the JLabel, you should replace it by: 如果要打印JLabel的文本,则应替换为:

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

Suggestions 建议

As you are asking for suggestions, the first thing you should do is to not set your class as as subclass of JFrame. 当您寻求建议时,您应该做的第一件事就是不要将您的类设置为JFrame的子类。 This is unnecessary and not flexible. 这是不必要的,也不灵活。 Just create a JFrame object and add your stuff in it. 只需创建一个JFrame对象,然后在其中添加内容即可。

Also, it is bad Java style to start a variable or field name by an uppercase letter, like you do with all your JCheckBox instances. 而且,像使用所有JCheckBox实例一样,用大写字母开头的变量或字段名称也是Java风格不好的做法。 See how that confuses StackOverflow's syntax highlighter. 了解这如何使StackOverflow的语法突出显示部分感到困惑。 Names with first capital letters are for classes, interfaces or enums. 首字母大写的名称用于类,接口或枚举。

Finally, you seem to have a bug: if you select first the pizza size and only afterwards the toppings, the total price will be the total price of the toppings only, without the price of the pizza itself. 最后,您似乎有一个错误:如果您先选择披萨尺寸,然后再选择浇头,则总价将仅是浇头的总价,而没有披萨本身的价格。

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 当我在Eclipse中运行程序时,它说有错误,但是它们没有像大多数错误在该程序中所示那样被标记在左边距上

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 . 您正在打印JLabel
What you see is not an error, rather it is the String representation of your JLabel . 您看到的不是错误,而是JLabelString表示形式。


How can I go about fixing this problem? 我该如何解决这个问题? Do you see anything in the program that I might want to change, ie 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 . 如果您想获得那就是在文本JLabel ,使用getText()方法JLabel
You would do it as shown below: 您将如下所示进行操作:

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

In this method 用这种方法

    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)));
}

You are saying System.out.println(totalPrice) . 您是在说System.out.println(totalPrice) So you are actually printing the textbox, not the data in the textbox. 因此,您实际上是在打印文本框,而不是文本框中的数据。 To do this, use System.out.println(totalPrice.getText()); 为此,请使用System.out.println(totalPrice.getText());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM