4

JOptionPaneに渡されたカスタムボタンによって返される値を取得しようとしています。ただし、渡すボタンは値をまったく返しません。終了ボタンが押された場合にのみ、-1の値が返されます。有効または無効にしたボタンのプロパティを変更しているので、これが必要です。何らかの方法でJOptionPaneに情報を返すためのボタンが必要だと思います。何か案が?

    JButton button1= new JButton("Button 1");
    JButton button2= new JButton("Button 2");

    button1.setEnabled(false);

    int value = JOptionPane.showOptionDialog(null, "Heres a test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1);
    JOptionPane.showMessageDialog(null, "You entered " + value);

Nbこれは私の前の質問に関連しています-JOptionPaneグレーアウトワンボタン

あなたが言ったようにボタンの値を設定しようとしましたが、OKまたはCANCELを返すことはありません。

ボタンの値をチェックするときはいつでも、私が設定した値を返すことはありません。

    JButton button1= new JButton("Button1");
    JButton button2= new JButton("Button2");

    button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane pane = getOptionPane((JComponent)e.getSource());
                // set the value of the option pane
                pane.setValue(JOptionPane.OK_OPTION);
            }
        });

    button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane pane = getOptionPane((JComponent)e.getSource());
                // set the value of the option pane
                pane.setValue(JOptionPane.CANCEL_OPTION);
            }
        });

      if (JOptionPane.showOptionDialog(null, "Pick a button", "Pick", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1) == JOptionPane.OK_OPTION) {
           JOptionPane.showMessageDialog(null, "Button1");
      }
      else{
           JOptionPane.showMessageDialog(null, "Button2");
      }

上記を参照してください。何があっても常にbutton2ポップアップが表示されます。

4

3 に答える 3

13

前の質問にリンクした例では、ボタンはJOptionPane#setValueメソッドを使用して戻り値を設定します。これにより、後からカスタマイズを提供しながら、通常どおりAPIを引き続き使用できます。

            final JButton okay = new JButton("Ok");
            okay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane pane = getOptionPane((JComponent)e.getSource());
                    // set the value of the option pane
                    pane.setValue(JOptionPane.OK_OPTION);
                }
            });

ユーザーが入力するまで、JOptionPane.dialogの[OK]ボタンを無効にするを詳しく見てください。

更新しました

コードに戻り、actionPerformedメソッドを修正して、有効な値を返すことができるようにしました...

final JButton okay = new JButton("Ok");
okay.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane pane = getOptionPane((JComponent)e.getSource());
        pane.setValue(okay);
    }
});
okay.setEnabled(false);
final JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane pane = getOptionPane((JComponent)e.getSource());
        pane.setValue(cancel);
    }
});

配列内の値のインデックスによって返される値options(最後のパラメーター)

だから、例えば...

int value = JOptionPane.showOptionDialog(
     null, 
     field, 
     "Get", 
     JOptionPane.YES_NO_OPTION, 
     JOptionPane.QUESTION_MESSAGE, 
     null, 
     new Object[]{okay, cancel}, 
     okay);

ユーザーが[OK]ボタンをクリックすると、戻り値はになります。ユーザーが0[キャンセル]ボタンを選択すると、次のようになります。1

于 2013-01-29T19:59:37.877 に答える
3

この複雑な動作が必要な場合は、独自のJDialogを作成し、それをモーダル形式で表示することを検討してください。

JOptionPaneを使用する必要がある場合は、JDialogを抽出し、無効にするコンポーネントが見つかるまでコンポーネントを再帰的に反復して、無効にすることでこれを行うことができます。

import java.awt.Component;
import java.awt.Container;
import javax.swing.*;

public class Foo2 {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            doRun();
         }
      });
   }

   public static void doRun() {
      String[] options = {"Button 1", "Button 2", "Button 3"};

      JOptionPane myOptionPane = new JOptionPane("Heres a test message",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, 
            null, options, options[2]);
      JDialog myDialog = myOptionPane.createDialog(null, "My Test");
      myDialog.setModal(true);

      inactivateOption(myDialog, options[1]);

      myDialog.setVisible(true);
      Object result = myOptionPane.getValue();
      // Note: result might be null if the option is cancelled
      System.out.println("result: " + result);

      System.exit(0); // to stop Swing event thread
   }

   private static void inactivateOption(Container container, String text) {
      Component[] comps = container.getComponents();
      for (Component comp : comps) {
         if (comp instanceof AbstractButton) {
            AbstractButton btn = (AbstractButton) comp;
            if (btn.getActionCommand().equals(text)) {
               btn.setEnabled(false);
               return;
            }
         } else if (comp instanceof Container) {
            inactivateOption((Container) comp, text);
         }
      }

   }
}

ただし、私自身はJDialogを作成するだけです。

于 2013-01-29T19:55:05.900 に答える
2

ボタンを明示的に定義する必要はありません。

int result = JOptionPane.showOptionDialog(this, "Are you sure you want to...?", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No" }, JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
  ...
}
于 2013-01-29T19:58:24.930 に答える