我得到了下面的代码:
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中运行此代码并在组合框中选择4(大于3),则jLabel1打印出"小于或等于3",即使它明显更大。有人能解释一下原因吗?
试试下面的代码。您使用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);
}