[英]How can I use the response from a JOptionPane?
This is the first time I have used a confirm box and I'd like some advice on how to use it please, I want to use the users input of "Yes or No" but not sure how to do it? 这是我第一次使用确认框,请问如何使用它的建议,我想使用用户输入的“是或否”,但不确定如何操作吗? If I wanted to reference the input from the JOptionPane in an if statement how would go about it? 如果我想在if语句中引用JOptionPane的输入,将如何处理?
JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);
Thank you. 谢谢。
use it like: 像这样使用它:
int result = JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);
if (JOptionPane.YES_OPTION == result) {
System.out.println("yes");
} else if (JOptionPane.NO_OPTION == result) {
System.out.println("No");
}else{
System.out.println("Nothing");
}
Also find the option types and return values below(from source): 还可以在下面找到选项类型并返回值(来自源):
/**
* Type meaning Look and Feel should not supply any options -- only
* use the options from the <code>JOptionPane</code>.
*/
public static final int DEFAULT_OPTION = -1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_OPTION = 0;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_CANCEL_OPTION = 1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int OK_CANCEL_OPTION = 2;
//
// Return values.
//
/** Return value from class method if YES is chosen. */
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
/** Return value form class method if OK is chosen. */
public static final int OK_OPTION = 0;
/** Return value from class method if user closes window without selecting
* anything, more than likely this should be treated as either a
* <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
public static final int CLOSED_OPTION = -1;
Also, don't do int check directly for response value, like if(1==result)
for NO_OPTION
, always use constants from the JoptionPane
class. 另外,不要直接对响应值进行int检查,例如对于NO_OPTION
if(1==result)
,请始终使用JoptionPane
类中的常量。
If you want to learn more about Swing Dialog then please have a look at the below section of Swing Tutorial where it is explained in detail along with a lot examples. 如果您想了解更多有关Swing对话框的信息,请查看Swing教程的以下部分,其中将详细解释它,并提供许多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.