java - SWT 窗口可在 Linux 上运行,但不能在 Mac 上运行

标签 java macos swt

我在我的项目中添加了一个 SWT 窗口

public class swtUI {
    protected Shell shell;
    protected Display display;

    public static void launch() {
        try {
            swtUI window = new swtUI();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();

        // create all controls
        blablabal
    }
}

并在main()中调用它作为

public static void main(String[] args) {
    swtUI.launch();
    System.out.println("\nComplete");   
}

在 Linux 上运行没有问题。 但是当我用 maxos swt.jar 替换 linux swt.jar 并在 mac 上运行时,出现异常:

    ***WARNING: Display must be created on main thread due to Cocoa restrictions.
org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.createDisplay(Unknown Source)
    at org.eclipse.swt.widgets.Display.create(Unknown Source)
    at org.eclipse.swt.graphics.Device.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.getDefault(Unknown Source)
    at edu.rockefeller.casanovaLab.csvCheck.CsvSuiteGUI.open(CsvSuiteGUI.java:129)
    at edu.rockefeller.casanovaLab.csvCheck.CsvSuiteGUI.launch(CsvSuiteGUI.java:119)
    at edu.rockefeller.casanovaLab.csvCheck.Program.main(Program.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

我对 SWT 还很陌生。我应该如何更改我的代码以使其在 Mac 上运行?

谢谢

最佳答案

this FAQ 中似乎提到了这一点。来自同一链接:

To work around this problem you'll have to pass the -XstartOnFirstThread option to the java executable as follows:

java -XstartOnFirstThread -cp swt.jar:. ControlExample

关于java - SWT 窗口可在 Linux 上运行,但不能在 Mac 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29355958/

相关文章:

java - 如何获取 flex-messaging-client-android.jar?

java - 一个对象的两个接口(interface)的命名约定

linux - 我可以使用对 munmap() 的单个调用来取消映射位于连续虚拟内存范围内的两个内存映射吗?

python - 如何让 ibm_db 或 PyDB2 python 模块与 Mac OS X 10.7 Lion 中的 DB2 配合使用?

java - 如何在 RCP 应用程序中保存选定的复选框?

java - 我应该处理从 EditorSupport 返回的 jface CellEditors

java - 在另一个模态 JFace 对话框之上打开模态 JFace 对话框

java - MapWritable 的 MapWritable 上的迭代器

java - 如何将InputStream从jsp传递到servlet

javascript - 禁用 Firefox 网络通知的默认警报声音