【发布时间】:2015-05-02 18:31:46
【问题描述】:
我的 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");
}
}
}
}
【问题讨论】:
标签: java string swing jcombobox