我是想让在点击程序那个关闭按钮后能出现确认消息,但是我添加的监听器总是在界面已经消失后才出现,那样不管你点击确认还是取消都将退出消息。
请问我该往哪里添加消息,或什么消息   shell.addShellListener(new ShellAdapter(){
   public void shellClosed(ShellEvent e){
   e.doit=    MessageDialog.openConfirm(Display.getCurrent.getActiveShell(), "退出", "确认退出");
}
});

解决方案 »

  1.   

    问题:
    是什么东西要触发这个事件,就跟他本身加监听,你的程序是给shell加了监听,而且是shell被关闭的时候触发,所以就有你说的现象
    解决办法:
    给你自己new的Button加监听就行了!
      比如说
      Button closeBtn = new Button(...);
      closeBtn.addSelectionListener(new SelectionListener() {
    public void widgetSelected(SelectionEvent event) {
            ...
            MessageDialog.openConfirm(Display.getCurrent.getActiveShell(), "退出", "确认退出");
            ...
    }
    public void widgetDefaultSelected(SelectionEvent arg0) {
    }
      });
      

  2.   

    最小化我已经找到所要重写的方法了 getShell().addShellListener(new ShellAdapter(){
        
         public void shellIconified(ShellEvent e){
         Display.getCurrent().getActiveShell().setVisible(false);
         }
        
        
        });但是关闭还是不行,我重写shellClosed方法,可是这样是在shell已经关闭的情况下,我是想在之前有确认消息,就像eclipse那样提示是否的确认对话框后
    才正式退出或不退出,那该重写那个方法呢??
      

  3.   

    难道没有知道eclipse是怎么实现呢?难道要上楼上那样实现鼠标监听器??
      

  4.   

    就重写shellClosed()方法就行了。把加监听这句放在shell.open ();之后。
    不过你弹对话框的时候有问题。因此你应该象下面这样写
    shell.addShellListener(new ShellAdapter(){
       public void shellClosed(ShellEvent e){
       e.doit= MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),"退出", "确认退出");
       }
    });
      

  5.   

    应用窗口?final Frame f = new Frame();
            f.setSize(100,100);
            f.addWindowListener(new WindowAdapter() {            @Override
                public void windowClosing(WindowEvent e) {
                    int num = JOptionPane.showConfirmDialog(f, "确定要退出吗?", "提示",
                            JOptionPane.OK_CANCEL_OPTION);
                    if (num == 0) {
                        System.exit(0);
                    }
                }
            });
            f.setVisible(true);
      

  6.   

    最小化时system tray提示信息import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.ShellListener;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.ToolTip;
    import org.eclipse.swt.widgets.Tray;
    import org.eclipse.swt.widgets.TrayItem;public class Test {    public static void main(String[] args) {
            Display display = new Display();
            Shell shell = new Shell(display);
            Image image = new Image(display, 16, 16);        final Tray tray = display.getSystemTray();
            final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON);        final TrayItem item = new TrayItem(tray, SWT.NONE);        toolTip.setText("亲爱的用户,我在这里");
            toolTip.setMessage("您看见了么,我我我.....");
            item.setToolTip(toolTip);
            item.setImage(image);        item.addListener(SWT.DefaultSelection, new Listener() {
                public void handleEvent(Event event) {                Shell s = event.display.getShells()[0];
                    s.setVisible(true);
                    s.setMinimized(false);
                }
            });        shell.addShellListener(new ShellListener() {
                public void shellDeactivated(org.eclipse.swt.events.ShellEvent e) {            }            public void shellActivated(org.eclipse.swt.events.ShellEvent e) {            }            public void shellClosed(org.eclipse.swt.events.ShellEvent e) {            }            public void shellDeiconified(org.eclipse.swt.events.ShellEvent e) {            }            public void shellIconified(org.eclipse.swt.events.ShellEvent e) {
                    ((Shell) e.getSource()).setVisible(false);
                    toolTip.setVisible(true);
                }        });        shell.setBounds(50, 50, 300, 200);
            shell.open();        while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            image.dispose();
            display.dispose();
        }
    }
      

  7.   

    在main中加一句:frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame是你的Frame或JFrame类的一个实例。
      

  8.   

    哇塞!一个简单的问题,各位!
      楼主写的是swt程序吧!怎么把JFrame都搬出来了//
    顺便问一句,楼主是在西安上学(xaut)的吗?
    也许我们是校友俄,我也在2002届,xaut出来的!
      

  9.   

    dracularking 9楼写的就是你要的:
       f.addWindowListener(new WindowAdapter() {            @Override
                 public void windowClosing(WindowEvent e) {
                 .....
                }
    });
      

  10.   

    上面的想法我早想到了,我早说过,那样重写方法,其实现是在窗口已经消失后才出现对话框,而且窗口消失了
    你使用Display.getCurrent().getActiveShell()方法是会出现异常的。各位,我写的是swt程序,用到的是JFACE,不是SWING!!
      

  11.   

    上面的想法我早想到了,我早说过,那样重写方法,其实现是在窗口已经消失后才出现对话框,而且窗口消失了
    你使用Display.getCurrent().getActiveShell()方法是会出现异常的。各位,我写的是swt程序,用到的是JFACE,不是SWING!!
      

  12.   


    恩,我是从xaut毕业的,03级
      

  13.   


    这个方法在纯粹的SWT环境没问题,但是在jface下却是已经消失了主界面后才出现对话框的。
    还有最小化方法我在之前的4楼已经回复,我现在需要的是重写关闭的,可是上述方法在Jface下并没有出现该有的结果
      

  14.   


    这句这么写:e.doit=MessageDialog.openConfirm(null,"退出","确认退出");其他的不变,就ok了!要求结账,给分!
      

  15.   

    你试了么? 主界面没有消失啊,谈出对话框你点的什么啊??Display.getCurrent.getActiveShell()是获得当前活动窗口.
    null应该是创建一个新的窗口.(解释的不一定正确)但肯定已经解决你的问题了.请照着写你测试一下再说? ok?
      

  16.   

    加监听的那一句保证在Display被dispose之前就可以了阿!
      给你一个实例,你看看,我已经试验成功的阿!肯定不会有问题。src------------------------------startpackage swt;import org.eclipse.jface.dialogs.MessageDialog;
    import org.eclipse.swt.events.ShellAdapter;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.widgets.*;public class ShellCloseConfirm {
    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setVisible(true);
    shell.setSize(200, 200);
    shell.open();
    shell.addShellListener(new ShellAdapter() {
    public void shellClosed(ShellEvent e) {
    e.doit = MessageDialog.openConfirm(Display.getCurrent()
    .getActiveShell(), "quit", "ok");
    }
    });
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }
    }
    src------------------------------end
      

  17.   


    看明白我说什么,OK?我早说了SWT中那样写正确,可我是要求写jface程序。import org.eclipse.jface.action.MenuManager;
    import org.eclipse.jface.action.StatusLineManager;
    import org.eclipse.jface.action.ToolBarManager;
    import org.eclipse.jface.dialogs.MessageDialog;
    import org.eclipse.jface.window.ApplicationWindow;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.ShellAdapter;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    public class Test extends ApplicationWindow { /**
     * Create the application window
     */
    public Test() {
    super(null);
    createActions();
    addToolBar(SWT.FLAT | SWT.WRAP);
    addMenuBar();
    addStatusLine();
    } /**
     * Create contents of the application window
     * @param parent
     */
    @Override
    protected Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);



    getShell().addShellListener(new ShellAdapter(){
    public void shellClosed(ShellEvent e){
    e.doit=MessageDialog.openConfirm(getShell(), "确定", "确认退出?");
    }
    });

    //
    return container;
    } /**
     * Create the actions
     */
    private void createActions() {
    // Create the actions
    } /**
     * Create the menu manager
     * @return the menu manager
     */
    @Override
    protected MenuManager createMenuManager() {
    MenuManager menuManager = new MenuManager("menu");
    return menuManager;
    } /**
     * Create the toolbar manager
     * @return the toolbar manager
     */
    @Override
    protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager toolBarManager = new ToolBarManager(style);
    return toolBarManager;
    } /**
     * Create the status line manager
     * @return the status line manager
     */
    @Override
    protected StatusLineManager createStatusLineManager() {
    StatusLineManager statusLineManager = new StatusLineManager();
    statusLineManager.setMessage(null, "");
    return statusLineManager;
    } /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
    try {
    Test window = new Test();
    window.setBlockOnOpen(true);
    window.open();
    Display.getCurrent().dispose();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Configure the shell
     * @param newShell
     */
    @Override
    protected void configureShell(Shell newShell) {
    super.configureShell(newShell);
    newShell.setText("New Application");
    } /**
     * Return the initial size of the window
     */
    @Override
    protected Point getInitialSize() {
    return new Point(500, 375);
    }}
    你试试会是象你要求的那样不????
      

  18.   

    各位同志,我再强调一次,我要求的是jface程序,不是SWT的。SWT我早承认那样添加代码没问题,OK???
      

  19.   

    楼主:ApplicationWindow close()的时候,是不掉shell的close方法的。
    [Note that in order to prevent recursive calls to this method it does not call Shell#close(). As a result ShellListeners will not receive a shellClosed event. ]
    在你的Test类里面覆盖ApplicationWindow 的close()方法,追加下面的代码就可以实现。 public boolean close() {
    getShell().close();
    return true;
    }请参考:
    http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/window/ApplicationWindow.html#close()
      

  20.   

    补充一下,覆盖后的close方法,不管是return true;还是return false;对话框逻辑都是对的。可能你会有疑问,不过我的解释是ApplicationWindow就属于Shell,Shell关闭了,窗口自然就关闭了!!!!!!
      

  21.   

    再强调一下,ApplicationWindow是属于Shell的下一层,如果不重写close方法的话,当他自己close的时候不是先去掉shell的close方法。因此他关闭之后shell自己也要关闭,shell关闭的时候shell的监听才会起作用。
      

  22.   

    感谢qingkangxu ,我明白了。具体操作就是要重写ApplicationWindow的close方法public boolean close(){

    return false;

    }如上述,然后在添加监听器方法就对了。protected Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);


    getShell().addShellListener(new ShellAdapter(){
    public void shellClosed(ShellEvent e){
    e.doit=MessageDialog.openConfirm(getShell(), "", "");
    }
    });

    //
    return container;
    }
    就对了。十分感谢!!
      

  23.   

    我晕, LZ我服了.
    不用swt 呵呵. 
    JFace 哪来的啊?  
    无语了...
      

  24.   

    再次感谢qingkangxu,结贴去了,分数给你在一楼
      

  25.   


    你自己去试试就知道怎么回事了。不要没试乱叫什么,不重写close。你添加监听器看看?没试不要乱叫,OK??
      

  26.   


    先去试试jface和swt的区别再来说话,OK??
    我一早就说过,用swt的shell添加监听器确实可以如希望的那样,用Jface你试试,程序在26楼贴出来了。你复制看看。别乱喊什么概念之类的东西,OK??
      

  27.   

    提醒楼主:
    请注意:
    http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/window/ApplicationWindow.html#close()里提示的内容,重写close方法去调用shell的close方法是有可能导致递归调用的!在某的环境下(具体我也说不清楚,只是看了url中的说明之后的感觉而已),使有可能发生recursive calls 的!
    楼主很牛啊 7点多就开始一天的工作了!!!!
      

  28.   

    不懂jface的呢就不要乱说了,耽误查资料的人的时间我看懂了楼主的意思。我来给你解决的方法解决方案:       在包含main函数的类中重写    handleShellCloseEvent() 方法     如:
         protected void handleShellCloseEvent()
        {
            setReturnCode(1);
       /**
                *  增加你想实现的代码(shell关闭前)
                */
            close();
        }