【问题标题】:Java - Questions about JOptionPane [duplicate]Java - 关于 JOptionPane 的问题 [重复]
【发布时间】:2013-03-05 17:30:45
【问题描述】:
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 中设置“否”按钮?

【问题讨论】:

  • 可能与stackoverflow.com/q/1395707/814074 重复/相似
  • @SachinPasalkar 不,我想要选择“否”按钮
  • 阅读上面帖子中用户提出的整个问题。
  • @SachinPasalkar 哎呀.......
  • 投票删除.......抱歉重复......

标签: java swing joptionpane


【解决方案1】:

使用JOptionPane.showOptionDialog 并设置options 和initialValue 参数。

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

【讨论】:

    【解决方案2】:

    使用这个构造函数:

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

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

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 2011-09-22
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多