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 Gray 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​​,同时为您提供自定义function。

  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上的Disable 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数组中value的索引返回的值(last参数)

所以,例如……

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

如果用户单击“okay”按钮,则返回值将为0 ,或者如果他们选择“取消”按钮,则为1

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

您不必明确定义按钮。

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