当我按下投诉框时,每个输入对话框都显示两次...我尝试删除 boxComplain.setSelected(true) 并且它有效(只显示一次)但是这使得复选框在我输入输入后消失.
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
if(boxComplain.isSelected())
{
ab=JOptionPane.showInputDialog("Enter Reason of Complain: ");
ac=JOptionPane.showInputDialog("Enter What The Complain is About: ");
label4.setText("Complain request");
boxComplain.setSelected(true);
}
}
}
最佳答案
ItemListener 被调用两次——一次是在原始选择被更改时,第二次是在新选择被注册时。考虑改用 ActionListener。
另一个技巧是删除和添加 ItemListener:
public void itemStateChanged(ItemEvent event) {
if(boxComplain.isSelected()) {
ab=JOptionPane.showInputDialog("Enter Reason of Complain: ");
ac=JOptionPane.showInputDialog("Enter What The Complain is About: ");
label4.setText("Complain request");
boxComplain.removeItemListener(this);
boxComplain.setSelected(true);
boxComplain.addItemListener(this);
}
}
关于java - JOptionPane.showInputDialog 显示两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16623650/