JOptionPane Grey Out One Button

◇◆丶佛笑我妖孽 提交于 2019-12-23 11:59:42

问题


I need to use a JOptionPane to give the user two options. Depending on previous actions though one of the buttons may need to be disabled.

Is it possible with JOptionPane to have the ability to set either of the buttons to be disabled or enabled?

How can I do this?


回答1:


It's easy if you use JButtons:

    public class Test
{
    public static void main(String[] args)
    {
        final JButton option1 = new JButton("option1");
        final JButton option2 = new JButton("option2");
        option1.setEnabled(false);
        // option2.setEnabled(false);
        option1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                // code here
            }
        });
        option2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // code here
            }
        });
        JOptionPane.showOptionDialog(null, "hello", "The Title", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, new JButton[]
        { option1, option2 }, option1);
    }
}


来源:https://stackoverflow.com/questions/14576196/joptionpane-grey-out-one-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!