如何让我的基本SWT应用程序在Mac OS X 10.5.6中正常退出?

Ale*_*lds 11 java macos swt

我有以下SWT测试代码:

public static void main(String[] args) {
    shell = new Shell();
    shell.setText(APP_NAME + " " + APP_VERSION);
    shell.addShellListener(new ShellListener() {
        public void shellActivated(ShellEvent event) { }
        public void shellClosed(ShellEvent event) { exit(); }
        public void shellDeactivated(ShellEvent event) { }
        public void shellDeiconified(ShellEvent event) { }
        public void shellIconified(ShellEvent event) { }
    });     
    shell.open();
    display = shell.getDisplay();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
Run Code Online (Sandbox Code Playgroud)

我的exit()方法如下:

private void exit() {
    System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)

我尝试通过关闭shell("窗口")或下拉应用程序菜单(标记为"SWT")并选择"退出"来退出应用程序.

当我这样做时,一个SWT存根留在Dock中,并且SWT应用程序实际上没有退出.我必须通过Eclipse或Force Quit手动终止SWT应用程序.

我已经尝试使用v3.4和v3.5 SWT jar,在Mac OS X 10.5.6(Intel)下的Eclipse 3.4.1下.

当我关闭shell时,是否需要做额外的工作才能退出应用程序?

McD*_*ell 9

您没有正确释放本机资源 - 您有资源泄漏.

你不需要这样做:

private void exit() {
    System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)

处理shell时,main方法将退出.如果必须使用exit方法,请在放置所有SWT资源后调用它:

    Display display = new Display();
    try {
        Shell shell = new Shell(display);
        try {
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } finally {
            if (!shell.isDisposed()) {
                shell.dispose();
            }
        }
    } finally {
        display.dispose();
    }
    System.exit(0);
Run Code Online (Sandbox Code Playgroud)