【发布时间】:2015-12-09 13:49:35
【问题描述】:
我的JButton 和JComboBox 有问题。
当我点击我的JButton 时,它什么也不做。
我希望点击后弹出JOptionPane 消息。
selectionBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"Insert", "Delete", "Find", "Update"
}));
selectionBox.setSelectedIndex(0);
selectionBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selection = selectionBox.getSelectedIndex();
switch (selection) {
case 0:
selection = 0;
}
}
});
processButton.setText("Process Request");
processButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (selection == 0) {
insertStudent();
}
}
});
public void insertStudent() {
int id;
String name;
String major;
id = Integer.parseInt(idTextField.getText());
name = nameTextField.getText();
major = majorTextField.getText();
student.getID(id);
student.getName(name);
student.getMajor(major);
JOptionPane.showMessageDialog(rootPane, "test");
}
【问题讨论】:
-
您确定选择变量为 0 吗?使用 insertStudent();没有 if 语句的方法并检查是否会显示 JOptionPane。
-
switch(selection){ case 0: selection = 0; }是不必要的......只是说。 -
在每个动作侦听器的末尾放置一个 System.out.println 以打印选择的值。