问题
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
回答1:
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.
回答2:
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
}
来源:https://stackoverflow.com/questions/4286679/why-does-joptionpane-getvalue-continue-to-return-uninitializedvalue