2

NotifyDescriptor を使用してポップアップ ダイアログ ボックスを作成する方法を学びました。PURCHASE私はとを読み取る 2 つの大きなボタンを持つ JPanel を設計しました。CASHOUT使用したコードは、下部に と を読み取る別の 2 つのボタンを表示していYesますNo。NotifyDescriptor に独自のボタンを画面に表示させたくありません。ボタンがそこにあるようにしたいだけで、カスタム ボタンの 1 つがクリックされると、ポップアップが閉じて値が保存されyesますno。私が使用しているコードは次のとおりです

       // パネルのインスタンスを作成し、JPanel を拡張します...
        ChooseTransactionType popupSelector = new ChooseTransactionType();

        // カスタム NotifyDescriptor を作成し、パネル インスタンスをパラメーター + 他のパラメーターとして指定します
        NotifyDescriptor nd = 新しい NotifyDescriptor(
                popupSelector, // パネルのインスタンス
                "Title", // ダイアログのタイトル
                NotifyDescriptor.YES_NO_OPTION, // はい/いいえダイアログです ...
                NotifyDescriptor.QUESTION_MESSAGE, // ... 質問タイプ => 疑問符アイコン
                null, // YES_NO_OPTION を指定しました => null にすることができます, L&F で指定されたオプション,
                      // それ以外の場合は、次のようにオプションを指定します:
                      // new Object[] { NotifyDescriptor.YES_OPTION, ... など },
                NotifyDescriptor.YES_OPTION // デフォルトのオプションは「はい」
        );

        // ダイアログを表示しましょう...
        if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
            // ユーザーが [はい] をクリックした場合、ここで何かを行います。たとえば、次のようにします。
                System.out.println(popupSelector.getTRANSACTION_TYPE());
        }
4

1 に答える 1

5

optionsボタンのテキストを置き換えるにはString[]options引数にaを渡すか、もう少し制御が必要な場合は、を渡すことができますJButton[]。したがって、あなたの場合、パネルからボタンを削除し、引数messageにaを渡す必要があります。String[]options

initialValue(最後の引数)には、を使用する代わりに、値(PurchaseまたはCashout)NotifyDescriptor.YES_OPTIONのいずれかを使用できます。String[]このDialogDisplayer.notify()メソッドは、選択された値を返します。したがって、この場合はaを返しStringますが、を渡すとJButton[]、戻り値は。になりますJButton

String initialValue = "Purchase";
String cashOut = "Cashout";
String[] options = new String[]{initialValue, cashOut};

NotifyDescriptor nd = new NotifyDescriptor(
            popupSelector,
            "Title",
            NotifyDescriptor.YES_NO_OPTION,
            NotifyDescriptor.QUESTION_MESSAGE,
            options,
            initialValue
    );

String selectedValue = (String) DialogDisplayer.getDefault().notify(nd);
if (selectedValue.equals(initialValue)) {
    // handle Purchase
} else if (selectedValue.equals(cashOut)) {
    // handle Cashout   
} else {
    // dialog closed with top close button
}
于 2012-04-25T01:28:00.993 に答える