Escolar Documentos
Profissional Documentos
Cultura Documentos
The basic approach for this can look like I've showed below:
1down vote accepted
if (n == JOptionPane.YES_OPTION) {
System.out.println("Yes");
} else if (n == JOptionPane.NO_OPTION) {
System.out.println("No");
} else if (n == JOptionPane.CLOSED_OPTION) {
System.out.println("Closed by hitting the cross");
}
}
}
public static void main(final String[] args) {
int value = 0;
while (true) {
try {
value = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value?", null));
if (value <= 0) {
System.out.println("value cannot be 0 or negative");
} else {
System.out.println("value is positive");
break;
}
} catch (NumberFormatException e) {
System.out.println("*** Please enter an integer ***");
}
}
}
int n = JOptionPane.showConfirmDialog(
frame, "Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
Alternatively with showOptionDialog:
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
EDIT: showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {