簡體   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