JOptionPane InputMismatchException

时间:2014-04-16 20:34:21

标签: java swing joptionpane

我正在构建一个简单的聊天应用程序,我想通过JOptionPane设置服务器地址和端口,我想要做的是检查用户在设置端口号时没有输入字母,

int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!");

我尝试使用try / catch块来处理它,但它没有用,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

NumberFormatException显然是答案,所以现在这个问题不会显示为未答复。

try{
    int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!");
}catch(NumberFormatException e){
   //handle error
}