我需要使用 aJOptionPane为用户提供两个选项。根据之前的操作,可能需要禁用其中一个按钮。
是否可以将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);
Run Code Online (Sandbox Code Playgroud)
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 …Run Code Online (Sandbox Code Playgroud)