我在下面提到了这段代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Integer intNumber = (Integer)jComboBox1.getSelectedIndex();
String text = null;
if (intNumber <= 3) {
text = "Less than or equal to three";
} else if (intNumber > 3) {
text = "Bigger than three";
}
jLabel1.setText(text);
}
但如果我在Netbeans中运行此代码并在Combobox中选择4(大于3),则jLabel1会打印出“小于或等于3”,即使它显然更大。有人能解释一下原因吗?
答案 0 :(得分:1)
试试这段代码。您使用getSelectedIndex()
代替getSelectedItem()
。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Integer intNumber = (Integer)jComboBox1.getSelectedItem();
String text = null;
if (intNumber <= 3) {
text = "Less than or equal to three";
} else if (intNumber > 3) {
text = "Bigger than three";
}
jLabel1.setText(text);
}