0
this.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                int par1 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit", "Exit?", JOptionPane.YES_NO_OPTION);
                if(par1==JOptionPane.YES_OPTION)
                {
                    System.exit(0);
                }
            }
        });

这是我的代码。如何在 JOptionPane requestFocus 中设置“否”按钮?

4

2 回答 2

2

使用JOptionPane.showOptionDialog并设置optionsinitialValue参数。

public static int showOptionDialog(Component parentComponent,
                                   Object message,
                                   String title,
                                   int optionType,
                                   int messageType,
                                   Icon icon,
                                   Object[] options,
                                   Object initialValue)
                            throws HeadlessException

试试这个:

Object[] options = { "YES", "NO" };
int par1 = JOptionPane.showOptionDialog(null, "Are you sure you want to exit", "Exit?",
    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
    null, options, options[1]);
if(par1==0)
{
     System.exit(0);
}

更多JOptionPane

于 2013-03-17T08:29:05.067 回答
0

使用此构造函数:

JOptionPane(Object message, int messageType, int optionType,
        Icon icon, Object[] options, Object initialValue)

options代表所有可用initialValue的按钮,是默认选择的按钮。

于 2013-03-17T08:24:38.030 回答