繁体   English   中英

如何使用JOptionPane的响应?

[英]How can I use the response from a JOptionPane?

这是我第一次使用确认框,请问如何使用它的建议,我想使用用户输入的“是或否”,但不确定如何操作吗? 如果我想在if语句中引用JOptionPane的输入,将如何处理?

JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);

谢谢。

像这样使用它:

   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");
    }

还可以在下面找到选项类型并返回值(来自源):

 /**
     * 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;

另外,不要直接对响应值进行int检查,例如对于NO_OPTION if(1==result) ,请始终使用JoptionPane类中的常量。

如果您想了解更多有关Swing对话框的信息,请查看Swing教程的以下部分,其中将详细解释它,并提供许多示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM