JOptionPane传递自定义按钮 [英] JOptionPane Passing Custom Buttons

查看:104
本文介绍了JOptionPane传递自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取传递给JOptionPane的自定义按钮返回的值。但是我传递的按钮根本不返回值。仅当按下退出按钮时,返回的值为-1。我需要这个,因为我正在更改启用或禁用按钮的属性。我假设我需要按钮以某种方式将一些信息返回给JOptionPane。有什么想法吗?

I'm trying to get the value returned by custom buttons passed to JOptionPane. However the buttons I pass don't return a value at all. Only when the exit button is pressed is a value of -1 returned. I need this because I am changing the properties of the buttons enabled or disabled. I assume I need the buttons to return some information to the JOptionPane in some way. Any idea?

    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 Gray Out One Button

Nb This is related to my previous question - JOptionPane Grey Out One Button

我尝试设置像你这样的按钮的值说但他们永远不会返回OK或CANCEL。

I tried setting the value of the buttons like you said but they never return OK or CANCEL.

每当检查按钮的值时,它们都不会返回我设置的值。

Whenever checking the value of the buttons, they never return the value I set them too.

    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弹出窗口。

See above, always I get the button2 popup no matter what.

推荐答案

在我上一个问题链接到的示例中,按钮使用 JOptionPane #setValue 设置返回值的方法。这允许您像往常一样继续使用API​​,同时为您提供自定义。

In the example I linked to you previous question, the buttons use the JOptionPane#setValue method to set the return value. This allows you to continue using the API as normal, while providing you with the customisation your after.

            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 使其能够返回有效值的方法...

I've gone back through the code and correct the actionPerformed methods to enable it to return a valid value...

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

选项中值的索引返回的值 array(最后一个参数)

The value returned by the index of the value in the options array (last parameter)

所以,例如......

So, for example...

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

如果用户点击了okay按钮,返回值将为 0 ,或者如果他们选择取消按钮,它将是 1

If the user clicks the okay button, the return value will be 0, or if they select the cancel button, it will be 1

这篇关于JOptionPane传递自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆