JFace应用程序中未触发关闭事件

时间:2016-01-22 00:27:53

标签: java jface

我是JFace的新手,我有一些问题要理解为什么我的亲密听众不能工作。 我修改了一个简单的JFace示例:

public class JFaceExample extends ApplicationWindow {
  private ExitAction exitAction;

  public JFaceExample() {
    super(null);
    exitAction = new ExitAction(this);
    addMenuBar();
    addStatusLine();
    addToolBar(SWT.FLAT | SWT.WRAP);
  }

  @Override
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("JFace File Explorer");
    shell.addListener(SWT.Show, new Listener() {
      @Override
      public void handleEvent(Event e) {
        System.out.println("show");
      }
    });
    shell.addListener(SWT.Close, new Listener() {
      @Override
      public void handleEvent(Event e) {
        System.out.println("close");
      }
    });
  }

  @Override
  protected Control createContents(Composite parent) {
    setStatus("JFace Example v1.0");
    return parent;
  }

  @Override
  protected MenuManager createMenuManager() {
    MenuManager menuBar = new MenuManager("");
    MenuManager fileMenu = new MenuManager("&File");
    MenuManager helpMenu = new MenuManager("&Help");
    menuBar.add(fileMenu);
    menuBar.add(helpMenu);
    fileMenu.add(exitAction);
    return menuBar;
  }

  @Override
  protected StatusLineManager createStatusLineManager() {
    StatusLineManager slm = new StatusLineManager();
    slm.setMessage("Hello, world!");
    return slm;
  }

  @Override
  protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager toolBarManager = new ToolBarManager(style);
    toolBarManager.add(exitAction);
    return toolBarManager;
  }

  public static void main(String[] args) {
    JFaceExample fe = new JFaceExample();
    fe.setBlockOnOpen(true);
    fe.open();
    Display.getCurrent().dispose();
  }

  private class ExitAction extends Action {
    ApplicationWindow window;

    public ExitAction(ApplicationWindow w) {
      window = w;
      setText("E&xit@Ctrl+W");
      setToolTipText("Exit the application");
    }

    @Override
    public void run() {
      window.close();
    }
  }
}

show message会成功写入控制台。但是,如果我单击退出菜单项,则应用程序将关闭,但不会写入关闭消息。

1 个答案:

答案 0 :(得分:0)

当您调用window.close()时,不会触发shell close事件,而是释放shell(因此您可以在shell上使用dispose侦听器)。

close代码中的源代码中有注释:

// If we "close" the shell recursion will occur.
// Instead, we need to "dispose" the shell to remove it from the
// display.
shell.dispose();