JOptionPane 取消按钮的处理,该按钮将字符串作为输入
Handling of JOptionPane cancel button, which takes string as input
我想通过输入对话框从用户那里获取字符串形式的输入,并在用户按下取消按钮时处理这种情况。
有什么建议吗?
您可以使用 class JOptionPane
的 showInputDialog 方法。
如果用户点击取消,返回值为null
。
另请注意,正如@mKorbel 在评论中所说,如果 windows 已直接关闭,您也会得到 null
。
String result = JOptionPane.showInputDialog("Please enter something");
if(result == null){
System.out.println("User pressed CANCEL, or window has been closed");
}
else{
// do something with the String
}
试试这个:
if(result == null){
System.out.println("User pressed CANCEL, or window has been closed");
System.exit(0);
}
我想通过输入对话框从用户那里获取字符串形式的输入,并在用户按下取消按钮时处理这种情况。
有什么建议吗?
您可以使用 class JOptionPane
的 showInputDialog 方法。
如果用户点击取消,返回值为null
。
另请注意,正如@mKorbel 在评论中所说,如果 windows 已直接关闭,您也会得到 null
。
String result = JOptionPane.showInputDialog("Please enter something");
if(result == null){
System.out.println("User pressed CANCEL, or window has been closed");
}
else{
// do something with the String
}
试试这个:
if(result == null){
System.out.println("User pressed CANCEL, or window has been closed");
System.exit(0);
}