选择Java中ShowMessage对话框右上角的x按钮后退出系统?

选择Java中ShowMessage对话框右上角的x按钮后退出系统?,java,joptionpane,Java,Joptionpane,我想知道如何在选择showMessageDialog对话框的X按钮时使程序退出。 目前,每当我这样做时,它只是继续运行代码,或者在确认或选项对话框中,选择“是”选项。是否可以在对话框的代码中包含此类命令?例如: JOptionPane.showMessageDialog(null, "Your message here"); 如何编辑输出,使X按钮关闭程序 我必须将showMessageDialog更改为另一种类型的对话框吗?我不知道这是否是您想要的,但我在程序中设置了一个确认框: (

我想知道如何在选择showMessageDialog对话框的X按钮时使程序退出。 目前,每当我这样做时,它只是继续运行代码,或者在确认或选项对话框中,选择“是”选项。是否可以在对话框的代码中包含此类命令?例如:

JOptionPane.showMessageDialog(null, "Your message here");
如何编辑输出,使X按钮关闭程序


我必须将showMessageDialog更改为另一种类型的对话框吗?

我不知道这是否是您想要的,但我在程序中设置了一个确认框:

    (...)
    import org.eclipse.swt.widgets.MessageBox;
    (...)
    createButton(buttons, "&Exit", "Exit", new MySelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent evt) {
            MessageBox messageBox = new MessageBox(getShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
            messageBox.setMessage("Are you sure?");
            messageBox.setText("Exit");
            if (messageBox.open() == SWT.YES) {
                getParent().dispose();
            }
        }
    });
看看or,你还有另一个选择:

直接使用:
要直接创建和使用JOptionPane,标准模式大致如下:

JOptionPane=newjoptionpane(参数);
pane.set.Xxxx(…);//配置
JDialog dialog=pane.createDialog(parentComponent,title);
dialog.show();
Object selectedValue=pane.getValue();
如果(selectedValue==null)
返回关闭的_选项;
//如果没有选项按钮数组:
如果(选项==null){
如果(整数的selectedValue实例)
返回((整数)selectedValue).intValue();
返回关闭的_选项;
}
//如果有一系列选项按钮:
对于(int counter=0,maxCounter=options.length;
计数器<最大计数器;计数器++){
if(选项[计数器].equals(selectedValue))
返回计数器;
}
返回关闭的_选项;
showMessageDialog()
没有返回值。下面是一个带有
showOptions对话框()
的示例

 JOptionPane pane = new JOptionPane(arguments);
 pane.set.Xxxx(...); // Configure
 JDialog dialog = pane.createDialog(parentComponent, title);
 dialog.show();
 Object selectedValue = pane.getValue();
 if(selectedValue == null)
   return CLOSED_OPTION;
 //If there is not an array of option buttons:
 if(options == null) {
   if(selectedValue instanceof Integer)
      return ((Integer)selectedValue).intValue();
   return CLOSED_OPTION;
 }
 //If there is an array of option buttons:
 for(int counter = 0, maxCounter = options.length;
    counter < maxCounter; counter++) {
    if(options[counter].equals(selectedValue))
    return counter;
 }
 return CLOSED_OPTION;
public class Test
{
    public static void main(String[] args){
        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Test");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int result = JOptionPane.showOptionDialog(null, 
                        "Your message here", "", JOptionPane.DEFAULT_OPTION, 
                        JOptionPane.PLAIN_MESSAGE, null, new String[] {"OK"}, "OK");
                if (result == JOptionPane.CLOSED_OPTION) {
                    frame.dispose();
                }
            }
        });

        panel.add(button);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }
}