Java: getSelectedItem() 未定义字符串类型

Java: getSelectedItem() is undefined for type string

我的 java 程序有问题,我正在尝试使用 JComboBox 和 JButton 获取 if 语句的一些信息。问题是 .getSelectedItem() 未定义,我不知道该怎么做。 这些是组合框:

static String JCBDestinations, JCBNights, JCBAccomodation;
static String[] places, nights, stay;

//Destination drop down menu
    String[] JCBDestinations = { " ", "Paris", "Crete", "Croatia"};
    JComboBox places = new JComboBox(JCBDestinations);
    places.setSelectedIndex(4);
    places.addActionListener(this);

//Number of nights radio buttons
    String[] JCBNights = { " ", "7", "10", "14"};
    JComboBox nights = new JComboBox(JCBNights);
    nights.setSelectedIndex(4);
    nights.addActionListener(this);

//Accommodation type drop down menu
    String[] JCBAccomodation = {" ", "Hotel", "Villa", "Youth Hostel", "Bed & Breakfast"};
    JComboBox stay = new JComboBox(JCBAccomodation);
    stay.setSelectedIndex(4);
    stay.addActionListener(this);

//Find deal button
    JBFind = new JButton("Find Deal"); //Adding option 1 button
    window.add(JBFind);
    JBFind.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {

        }
    });

这是 If 语句:

public void actionPerformed(ActionEvent e) 
{

    if (e.getSource() == JBFind);
    {
        System.out.println("Calculating cost");

        JLBeforeVAT.setText("£499");
        JTAfterVAT.setText("£589");
    }   
            if (JCBDestinations.getSelectedItem().equals("Paris"))
            {
                if (JCBNights.getSelectedItem().equals("7"))
                {
                    if (JCBAccomodation.getSelectedItem().equals("Hotel"))
                    {
                        JLBeforeVAT.setText("£499");
                        JTAfterVAT.setText("£589");
                    }

                }

            }
}

错误消息确切地告诉你你做错了什么,所以这里的关键是仔细阅读它,并修正它所显示的错误。您从 JComboBox 中获得选择,而不是从 String 数组中获得。组合框变量名为 places。所以

if (places.getSelectedItem().equalsIgnoreCase("Paris"))

其他问题:

String[] JCBNights = { " ", "7", "10", "14"};
JComboBox nights = new JComboBox(JCBNights);
nights.setSelectedIndex(4);  // *****
nights.addActionListener(this);

您正在将选定的索引设置为 4,当它只上升到 3 时:0、1、2、3。