我收到错误:<identifier expected =“”>和错误:从addItem语句非法启动ComboBox类型</identifier>

时间:2014-04-27 22:27:22

标签: java eclipse events combobox

我有一个Java类的项目,需要我添加到JComboBoxes并设置事件,以便在点击它们时根据需要更改比萨饼价格。另外我知道事件代码有些不正确。它需要处理两个组合框,而且有些不正确。我很难理解我需要改变什么。这是代码:

// File name JPizza.java
// Written by Me
// Written on 4-26-2014

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JPizza extends JFrame implements ActionListener
{
    final int EXTRALARGE_PRICE = 14;
    final int LARGE_PRICE = 11;
    final int MEDIUM_PRICE = 9;
    final int SMALL_PRICE = 7;

    final int CHEESE_PRICE = 0;
    final int PEPPERONI_PRICE = 1;
    final int SAUSAGE_PRICE = 1;
    final int REDONION_PRICE = 1;
    final int GREENPEPPER_PRICE = 1;
    final int MUSHROOM_PRICE = 1;

    int totalPrice = EXTRALARGE_PRICE + CHEESE_PRICE;

    JLabel applicationLabel = new JLabel("Pizza Price Calculator");

    JLabel optionExplainLabel = new JLabel("Select what size, and what topping you want from the drop down menus. Prices are listed in the combo boxes.");

    JLabel pizzaSize = new JLabel("Pizza size");
    JComboBox pizzaSizeValue = new JComboBox();
        pizzaSizeValue.addItem("Extra Large - $14");
        pizzaSizeValue.addItem("Large - $11");
        pizzaSizeValue.addItem("Medium - $9");
        pizzaSizeValue.addItem("Small - $7");
        pizzaSizeValue.setSelectedItem("Extra Large - $14");

    JLabel pizzaTopping = new JLabel ("Pizza topping");
    JComboBox pizzaToppingValue = new JComboBox();
        pizzaToppingValue.addItem("Cheese - free");
        pizzaToppingValue.addItem("Pepperoni - $1");
        pizzaToppingValue.addItem("Sausage - $1");
        pizzaToppingValue.addItem("Red Onion - $1");
        pizzaToppingValue.addItem("Green Pepper - $1");
        pizzaToppingValue.addItem("Mushroom - $1");
        pizzaToppingValue.setSelectedItem("Cheese - free");

    JLabel priceLabel = new JLabel("The price for your pizza is");
    JTextField totPrice = new JTextField(3);


    public JPizza()
    {
        super("Pizza Price Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        add(applicationLabel);
        add(optionExplainLabel);
        add(pizzaSize);
        add(pizzaSizeValue);
        add(pizzaTopping);
        add(pizzaToppingValue);
        add(priceLabel);
        add(totPrice);
        totPrice.setText("$" + totalPrice);
        pizzaSizeValue.addActionListener(this);
        pizzaToppingValue.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event)
    {
        int pizzaSizeCost = 14;
        int pizzaToppingCost = 0;
        JComboBox comboBox = (JComboBox) event.getSource();
        Object source = event.getSource();
        Object select = comboBox.getSelectedItem();
        if(source == pizzaSizeValue)
        {
            if(select.equals("Extra Large - $14"))
                pizzaSizeCost = EXTRALARGE_PRICE;
            if(select.equals("Large - $11"))
                pizzaSizeCost = LARGE_PRICE;
            if(select.equals("Medium - $9"))
                pizzaSizeCost = MEDIUM_PRICE;
            if(select.equals("Small - $7"))
                pizzaSizeCost = SMALL_PRICE;
        }
        if(source == pizzaToppingValue)
        {
            if(select.equals("Cheese - free"))
                pizzaSizeCost = CHEESE_PRICE;
            if(select.equals("Pepperoni $1"))
                pizzaSizeCost = PEPPERONI_PRICE;
            if(select.equals("Sausage $1"))
                pizzaSizeCost = SAUSAGE_PRICE;
            if(select.equals("Red Onion $1"))
                pizzaSizeCost = REDONION_PRICE;
            if(select.equals("Green Pepper $1"))
                pizzaSizeCost = GREENPEPPER_PRICE;
            if(select.equals("Mushroom $1"))
                pizzaSizeCost = MUSHROOM_PRICE;
        }       
    }

    public static void main(String[] args)
    {
        JPizza aFrame =
            new JPizza();
            final int WIDTH = 300;
            final int HEIGHT = 200;
            aFrame.setSize(WIDTH, HEIGHT);
            aFrame.setVisible(true);
    }
}

以下是错误:

c:\Java\Homework\CH14\YouDoIt>javac JPizza.java
JPizza.java:31: error: <identifier> expected
                pizzaSizeValue.addItem("Extra Large - $14");
                                      ^
JPizza.java:31: error: illegal start of type
                pizzaSizeValue.addItem("Extra Large - $14");
                                       ^
JPizza.java:32: error: <identifier> expected
                pizzaSizeValue.addItem("Large - $11");
                                      ^
JPizza.java:32: error: illegal start of type
                pizzaSizeValue.addItem("Large - $11");
                                       ^
JPizza.java:33: error: <identifier> expected
                pizzaSizeValue.addItem("Medium - $9");
                                      ^
JPizza.java:33: error: illegal start of type
                pizzaSizeValue.addItem("Medium - $9");
                                       ^
JPizza.java:34: error: <identifier> expected
                pizzaSizeValue.addItem("Small - $7");
                                      ^
JPizza.java:34: error: illegal start of type
                pizzaSizeValue.addItem("Small - $7");
                                       ^
JPizza.java:35: error: <identifier> expected
                pizzaSizeValue.setSelectedItem("Extra Large - $14");
                                              ^
JPizza.java:35: error: illegal start of type
                pizzaSizeValue.setSelectedItem("Extra Large - $14");
                                               ^
JPizza.java:39: error: <identifier> expected
                pizzaToppingValue.addItem("Cheese - free");
                                         ^
JPizza.java:39: error: illegal start of type
                pizzaToppingValue.addItem("Cheese - free");
                                          ^
JPizza.java:40: error: <identifier> expected
                pizzaToppingValue.addItem("Pepperoni - $1");
                                         ^
JPizza.java:40: error: illegal start of type
                pizzaToppingValue.addItem("Pepperoni - $1");
                                          ^
JPizza.java:41: error: <identifier> expected
                pizzaToppingValue.addItem("Sausage - $1");
                                         ^
JPizza.java:41: error: illegal start of type
                pizzaToppingValue.addItem("Sausage - $1");
                                          ^
JPizza.java:42: error: <identifier> expected
                pizzaToppingValue.addItem("Red Onion - $1");
                                         ^
JPizza.java:42: error: illegal start of type
                pizzaToppingValue.addItem("Red Onion - $1");
                                          ^
JPizza.java:43: error: <identifier> expected
                pizzaToppingValue.addItem("Green Pepper - $1");
                                         ^
JPizza.java:43: error: illegal start of type
                pizzaToppingValue.addItem("Green Pepper - $1");
                                          ^
JPizza.java:44: error: <identifier> expected
                pizzaToppingValue.addItem("Mushroom - $1");
                                         ^
JPizza.java:44: error: illegal start of type
                pizzaToppingValue.addItem("Mushroom - $1");
                                          ^
JPizza.java:45: error: <identifier> expected
                pizzaToppingValue.setSelectedItem("Cheese - free");
                                                 ^
JPizza.java:45: error: illegal start of type
                pizzaToppingValue.setSelectedItem("Cheese - free");
                                                  ^
24 errors

Any help would be appreciated!

1 个答案:

答案 0 :(得分:0)

在任何构造函数或方法之外的类中,可以发现违规代码,并且它不存在。那里只能存在变量或类型声明和初始化。您的违规代码应该位于您班级的构造函数中。

请注意,此问题与JComboBoxes甚至Swing几乎没有关系,而且与您尝试对方法或构造函数之外的变量进行方法调用的常见Java问题有关。你可以在课堂上声明和初始化变量,但你不能在那里进行方法调用。