java - JOptionPane 传递自定义按钮

标签 java swing jbutton joptionpane

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

最佳答案

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

关于java - JOptionPane 传递自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14591089/

相关文章:

java - 为什么我的代码有这个错误

java - swing JTree、JTable 或 JList 中的 Activity 元素

java - MouseListener 无法识别第一次点击

java - SearchEventListener 不是抽象的,不会重写抽象方法 actionPerformed

java - Spring3 - Autowiring 没有发生。该对象为空

java - 如何添加一个计时器对象以在一个线程上每 X 秒执行一次操作,在另一个线程上每 Y 秒执行一次操作?

java - 使用 shell 文件运行 jar 并指定类路径和属性文件路径

Java Swing : JWindow appears behind all other process windows, 并且不会消失

java - 创建可点击的 JButton 矩阵

java - jPanel 和 jButton 自定义