I want to know how to cause a program to exit upon selecting the X button of a showMessageDialog dialog box. Currently whenever I do this, it simply continues running the code or, in the case of confirm or option dialog boxes, selects the 'Yes' option. Is it possible to include this kind of command in the code for the dialog box? For example:

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

JOptionPane.showMessageDialog(null, "Your message here");

How would I edit the output so that the X button closes the program?

如何编辑输出以便X按钮关闭程序?

Will I have to change the showMessageDialog to another type of dialog box?

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

2 个解决方案

#1


0

I dont know if is this what you want but i put a confirm box in a program:

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

    (...)
    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();
            }
        }
    });

And looking at online javadoc (java 6) or (java 1.4), you have another option:

看看在线javadoc(java 6)或(java 1.4),你有另一种选择:

Direct Use:
To create and use an JOptionPane directly, the standard pattern is roughly as follows:

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

 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;

更多相关文章

  1. 2016年团体程序设计天梯赛-初赛 - 重要的话说三遍 - Java
  2. java程序运行机制和jvm
  3. java的学习方法(转自黑马程序员)
  4. 黑马程序员 Java中根据YYYY-MM-DD格式的日期计算为星期几的两种
  5. 黑马程序员-Java基础总结11——IO流(二)
  6. Java借助Runtime调用外部程序阻塞的代码
  7. Java:创建程序以查找圆柱体的表面积和体积

随机推荐

  1. Android WebView获取上一个链接的路径
  2. android画图-解锁界面
  3. Android——TabWidget
  4. Android ViewHolder 优化 ListView
  5. Android倒计时器——CountDownTimer
  6. Android 如何加载大图片
  7. 在Android线程中设置控件的值会报错
  8. Android TabLayout导航条属性的设置
  9. Android开发遇到的问题
  10. Android 检测键盘是否显示及隐藏键盘