在我链接到您上一个问题的示例中,按钮使用该
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); } });仔细查看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数组中的值的索引返回的值(最后一个参数)
举例来说…
int value = JOptionPane.showOptionDialog( null, field, "Get", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{okay, cancel}, okay);如果用户单击“确定”按钮,则返回值为
0,或者,如果他们选择“取消”按钮,则返回值为
1



