gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:26:47 24 4
gpt4 key购买 nike

我的 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 中获取选择,而不是从字符串数组中获取。组合框变量被命名为地方。所以

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

其他问题:

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

当所选索引仅达到 3:0、1、2、3 时,您将其设置为 4。

关于Java: getSelectedItem() 对于字符串类型未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com