3
Object[] options = {"questions", "list"};

Object selection = JOptionPane.showOptionDialog(Main.mWindow, "newDocText", "newDoc",
JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

上記のコードを使用してJOptionPane.

選択したプライマリ オプションにフォーカスが置かれていますが、完全に非表示にしたいと考えています。それは可能ですか?

4

2 に答える 2

3

options[0]useを使用する代わりにnull:

Object[] options = {"questions", "list"};

Object selection = JOptionPane.showOptionDialog(Main.mWindow, "newDocText", "newDoc",
JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,null);

Javaドキュメントに従って

showOptionDialog

public static int showOptionDialog(Component parentComponent,
                                   Object message,
                                   String title,
                                   int optionType,
                                   int messageType,
                                   Icon icon,
                                   Object[] options,
                                   Object initialValue)

initialValue - ダイアログのデフォルトの選択を表すオブジェクト。オプションが使用されている場合にのみ意味があります。ヌルにすることができます

于 2012-09-05T14:47:21.530 に答える
3

私にとって、David Kroukampの答えは、おそらくフォーカスのあるコンポーネントが常に存在する必要があるため、最初のボタンがフォーカスされたままになります。次のコードは、JLabel に明示的にフォーカスを与えます。

    JLabel message = new JLabel("newDocText");
    final JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, options);
    JDialog dialog = pane.createDialog(f, "newDoc");
    message.requestFocus();
    dialog.setVisible(true);
    Object selection = pane.getValue();

編集: フォーカスのペイントのみが問題である場合は、 setFocusPainted(false) を呼び出した後、JButtons を JOptionPane に渡すことができます。次のように実行できます。

    JButton questionsButton = new JButton("questions");
    JButton listButton = new JButton("list");
    questionsButton.setFocusPainted(false);
    listButton.setFocusPainted(false);
    Object[] options = {questionsButton, listButton};

ただし、この場合、ダイアログを閉じるように設定する必要があります。これはより複雑な解決策だと思います。

于 2012-09-05T15:16:55.730 に答える