【问题标题】:JOptionPane InputMismatchExceptionJOptionPane 输入不匹配异常
【发布时间】:2014-04-16 20:34:20
【问题描述】:

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

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

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

【问题讨论】:

  • “我试图用 try/catch 块处理它,但没有用”是什么意思?
  • try{code}catch(inputmismatchexception){}@BackSlash
  • 什么没用?
  • 它显示了一个错误,但是我刚刚修复了它,无论如何谢谢 :) 而不是 inputmismatchexception 它需要 numberformat 异常

标签: java swing joptionpane


【解决方案1】:

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

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

【讨论】:

  • 你知道有什么方法可以让 JOptionPane 显示出来,直到你输入正确的结果?我的意思是如果你输入一个字母,提示一个错误消息然后重新输入?
  • 我为此使用了 DocumentFilter。它工作得很好。这是一个示例的链接:stackoverflow.com/questions/11093326/…
猜你喜欢
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多