【问题标题】:JOptionPane Passing Custom ButtonsJOptionPane 传递自定义按钮
【发布时间】:2013-01-13 12:09:22
【问题描述】:

我正在尝试获取传递给 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。

【问题讨论】:

  • 请参阅编辑回答。

标签: java swing jbutton joptionpane


【解决方案1】:

在我链接到您上一个问题的示例中,按钮使用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);
    }
});

options数组中值的索引返回的值(最后一个参数)

所以,例如...

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

如果用户点击确定按钮,返回值为0,如果用户点击取消按钮,返回值为1

【讨论】:

  • 每当检查按钮的值时,它们也不会返回我设置的值。
  • 现在发表在原始问题中。
  • 我现在不在电脑前,有机会我会看看
【解决方案2】:

如果您需要这种复杂的行为,请考虑创建自己的 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。

【讨论】:

    【解决方案3】:

    您不必明确定义按钮。

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

    【讨论】:

    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多