JOptionPane传递自定义按钮

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

注:这与我之前的问题有关 - JOptionPane Grey Out One Button

我尝试按照您说的设置按钮的值,但它们从未返回“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弹出窗口。


请参见答案编辑。 - Hovercraft Full Of Eels
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);
                }
            });

请仔细查看Disable ok button on JOptionPane.dialog until user gives an input

更新

我重新审查了代码并更正了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);
    }
});
< p > options 数组中值的索引(最后一个参数)返回的值。

因此,例如...

int value = JOptionPane.showOptionDialog(
     null, 
     field, 
     "Get", 
     JOptionPane.YES_NO_OPTION, 
     JOptionPane.QUESTION_MESSAGE, 
     null, 
     new Object[]{okay, cancel}, 
     okay);
如果用户点击“确认”按钮,返回值将是0;如果选择“取消”按钮,则为1

每当检查按钮的值时,它们从未返回我设置的值。 - user2020457
已在原问题中发布。 - user2020457
我现在不在电脑旁,有机会时我会看一下。 - MadProgrammer

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。

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) {
  ...
}

这个问题与OP之前的问题有关-https://dev59.com/6m3Xa4cB1Zd3GeqPislN,因此他们正在尝试传递自己的按钮。 - MadProgrammer
是的,我刚才认出了这种问题的风格 ;) - MadProgrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接