Java JOptionPane取消按钮不存在';我不能正常工作
我正在与JOptionPane合作。当用户单击“确定”按钮时效果非常好,但当单击“取消”按钮以及关闭消息时,它会显示错误消息Java JOptionPane取消按钮不存在';我不能正常工作,java,swing,user-interface,jframe,joptionpane,Java,Swing,User Interface,Jframe,Joptionpane,我正在与JOptionPane合作。当用户单击“确定”按钮时效果非常好,但当单击“取消”按钮以及关闭消息时,它会显示错误消息 private void findActionPerformed(java.awt.event.ActionEvent evt) { try { String num = JOptionPane.showInputDialog("Number to Search:");
private void findActionPerformed(java.awt.event.ActionEvent evt) {
try {
String num = JOptionPane.showInputDialog("Number to Search:");
int number = Integer.parseInt(num);
s.search(number);
}catch (Exception e) {
JOptionPane.showMessageDialog(this, "WRONG INPUT: you must insert integers", "Erorr", JOptionPane.ERROR_MESSAGE);
}
}
使用
JOptionPane
在关闭或按取消时返回null
。因此,只需检查这一点,如上面的代码所示。当您将null
传递给Integer.parseInt()
时,将引发异常
private void findActionPerformed(java.awt.event.ActionEvent evt) {
try {
String num = JOptionPane.showInputDialog("Number to Search:");
if(num != null)
{
int number = Integer.parseInt(num);
s.search(number);
}
}catch (Exception e) {
JOptionPane.showMessageDialog(this, "WRONG INPUT: you must insert integers", "Erorr", JOptionPane.ERROR_MESSAGE);
}
}