Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理JOptionPane取消按钮,该按钮将字符串作为输入_Java_Swing_Joptionpane - Fatal编程技术网

Java 处理JOptionPane取消按钮,该按钮将字符串作为输入

Java 处理JOptionPane取消按钮,该按钮将字符串作为输入,java,swing,joptionpane,Java,Swing,Joptionpane,我想通过一个输入对话框从用户那里获取字符串形式的输入,并在用户按下取消按钮时处理这种情况 有什么建议吗 您可以使用classJOptionPane的方法 如果用户点击Cancel,则返回值为null 另外请注意,正如@mKorbel在评论中所说,如果直接关闭窗口,您也会得到null String result = JOptionPane.showInputDialog("Please enter something"); if(result == null){ System.out

我想通过一个输入对话框从用户那里获取字符串形式的输入,并在用户按下取消按钮时处理这种情况

有什么建议吗

您可以使用class
JOptionPane
的方法

如果用户点击Cancel,则返回值为
null

另外请注意,正如@mKorbel在评论中所说,如果直接关闭窗口,您也会得到
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
}
您可以使用class
JOptionPane
的方法

如果用户点击Cancel,则返回值为
null

另外请注意,正如@mKorbel在评论中所说,如果直接关闭窗口,您也会得到
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);
}
试试这个:

if(result == null){
    System.out.println("User pressed CANCEL, or window has been closed");
    System.exit(0);
}

请参见
JOptionPane.showInputDialog
<如果用户选择“取消”,则返回code>null。@Berger如何将该评论升级为答案?为什么社区会尝试回答这样的问题。这类问题被问了数百万次,但仍然得到了答案。。只需用谷歌搜索你的问题。请参见
JOptionPane.showInputDialog
<如果用户选择“取消”,则返回code>null。@Berger如何将该评论升级为答案?为什么社区会尝试回答这样的问题。这类问题被问了数百万次,但仍然得到了答案。。用谷歌搜索你的问题。不同意,因为
result!=null
JOptionPane
在API中实现了所有的
windows事件
(关闭,退出键事件…)@mKorbel:没错,但是您是否同意获取
null
至少意味着没有选择
Ok
按钮?不同意,因为
result!=null
JOptionPane
在API中实现了所有的
windows事件
(关闭,从escape键开始的事件,…)@mKorbel:没错,但是您是否同意获取
null
至少意味着没有选择
Ok
按钮?