我只是新的学习OOP抱歉这个基本的职位。我不知道为什么当我试图获得我的JComboBox
中所选项目的值时,它返回null。为什么getSelectedItem返回null?
public class AddEmployee extends javax.swing.JInternalFrame{
public AddEmployee()
{
initComponents();
this.setSize(1100,500);
setMonths();
setJComboBoxProperties();
check();
}
private void setMonths()
{
String[] monthsObj = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
DefaultComboBoxModel monthsModel = new DefaultComboBoxModel(monthsObj);
cbMonths.setModel((ComboBoxModel)monthsModel);
}
private void setJComboBoxProperties()
{
cbMonths.setSelectedIndex(-1);
}
private String check()
{
String cb = (String)cbMonths.getSelectedItem();
System.out.println(cb);
return cb;
}
}
我投了String cb
所以它不会给我一个null。但我试图检查出选定的项目,但它给了我空。
cbMonth在哪里申报?在哪里选择一个条目? – Jens
@Jens Btw这是拖放。这就是为什么我没有创建'JComboBox'的实例:) –
拖放?你必须声明cbMounth。 – Jens