我刚刚学习 OOP,对这篇基本文章感到抱歉。我不知道为什么当我尝试获取所选项目的值时它返回 nullJComboBox
.
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
所以它不会给我一个空值。但我试图检查所选项目,但它给了我空值。
Call setSelectedItem
初始化组合框后。请参阅文档 https://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html#setSelectedItem(java.lang.Object).
DefaultComboBoxModel monthsModel = new DefaultComboBoxModel(monthsObj);
monthsModel.setSelectedItem('September');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)