在Swt中,如果要给当前窗口添加窗口关闭事件,只需要为shell添加ShellListener事件,但是在Jface中,对于窗口的关闭事件的处理却和swt中不太一样。我们在用Jface程序的时候,注程序要继承ApplicationWindow, 而该类继承了Window,在Window类中已经为我们定义好了处理窗口关闭事件的方法--handleShellCloseEvent(),我们只需重写这个方法,就可以实现对窗口关闭事件的处理。下面给出一段代码,在点击窗口关闭按钮后,弹出对话框提示用户是否退出。
/** 处理窗口关闭事件 */ @Override protected void handleShellCloseEvent() { MessageBox messagebox = new MessageBox(getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); messagebox.setMessage("您确定要退出吗?"); int message = messagebox.open(); if (message == SWT.YES) { super.handleShellCloseEvent(); } }