如何获取 JOptionPane 的退出值

How to get JOptionPane's exit value

抱歉,如果有人问过这个问题,但我在任何地方都找不到。我用 JSlider 定制了 JOptionPane。它做了它应该做的事,但我如何检查按下的是确定还是取消?因为现在它无论如何都会这样做。

代码:

JOptionPane optionPane = new JOptionPane();
JSlider slider = getSlider(optionPane);
slider.setValue(value);
optionPane.setMessage(new Object[]{"Select a value: ", slider});
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
optionPane.createDialog(parent, title).setVisible(true);

// get return value of optionPane

方法都找遍了,没找到,不代表;但是,它不存在。
任何帮助表示赞赏。谢谢!

documentation 有一个很好的例子说明了您创建对话框的方式。

文档的一小段:

JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
  return CLOSED_OPTION;
//If there is not an array of option buttons:
if(options == null) {
  if(selectedValue instanceof Integer)
     return ((Integer)selectedValue).intValue();
  return CLOSED_OPTION;
}

看起来 pane.getValue() 在您 show() 对话框之后会有结果值。

您可能希望重组代码以使用更易于使用的 showOptionDialog 方法,如 this related question 中所述。