2013-03-17 134 views
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中设置“否”按钮?Java - 关于JOptionPane的问题

+1

可能复制/相似http://stackoverflow.com/q/1395707/814074 – Sach 2013-03-17 08:36:01

+0

@SachinPasalkar没有,我希望“号“按钮选择 – 2013-03-17 10:44:05

+0

在上面的帖子中阅读用户提出的整个问题。 – Sach 2013-03-17 10:46:53

回答

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

+0

感谢帮助我 – 2013-03-17 10:45:06

0

使用这个构造:

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

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