为什么 JOptionPane.getValue() 继续返回 uninitializedValue

发布于 2024-10-04 18:21:28 字数 358 浏览 0 评论 0原文

下面是我的代码,

public static void main(String args[]){
     JOptionPane pane = new JOptionPane();
     pane.showInputDialog(null, "Question");
     Object value = value.getValue();
     System.out.println(value.toString()); --> this will print out uninitializedValue

}

我基本上想检测用户何时单击 JOptionPane 中的取消以及用户何时关闭 JOptionPane

Below are my code

public static void main(String args[]){
     JOptionPane pane = new JOptionPane();
     pane.showInputDialog(null, "Question");
     Object value = value.getValue();
     System.out.println(value.toString()); --> this will print out uninitializedValue

}

I basically want to detect when the user click the cancel in JOptionPane and when the user close the JOptionPane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-10-11 18:21:28

您应该这样做:

    String s = JOptionPane.showInputDialog(null, "Question");
    System.out.println(s);

如果关闭窗格或按下“取消”,这将返回 null 字符串。

You should do this:

    String s = JOptionPane.showInputDialog(null, "Question");
    System.out.println(s);

This will return a null string if the pane is closed or Cancel is pressed.

眼前雾蒙蒙 2024-10-11 18:21:28

showInputDialog 是一个静态方法,它不会修改JOptionPane。正如dogbane 指出的那样,您应该检查返回值showInputDialog

如果您在实例上调用静态方法,某些编译器会生成警告,因此请务必检查编译器警告。在你的情况下调用这样的方法:

String result = JOptionPane.showInputDialog(null, "Question");
if(result == null){
//chancel pressed
}else{
//normal code
}

showInputDialog is a static method, it does not modify the JOptionPane. As dogbane points out you should check the return value showInputDialog.

Some compilers generate warnings if you call static methods on instances, so always check compiler warnings. In your case call the method like this:

String result = JOptionPane.showInputDialog(null, "Question");
if(result == null){
//chancel pressed
}else{
//normal code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文