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

4

2 回答 2

3

你应该做这个:

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

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

于 2010-11-26T15:59:18.997 回答
2

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

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

String result = JOptionPane.showInputDialog(null, "Question");
if(result == null){
//chancel pressed
}else{
//normal code
}
于 2010-11-26T16:08:15.777 回答