我在我的项目中添加了一个 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/