package com.samples.swt.first; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Main { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }
当我从Eclipse Helios在Mac OSX上运行它时,我得到以下错误:
***WARNING: Display must be created on main thread due to Cocoa restrictions.
Exception in thread “main” org.eclipse.swt.SWTException:
Invalid thread access at org.eclipse.swt.SWT.error(SWT.java:4282) at
org.eclipse.swt.SWT.error(SWT.java:4197) at
org.eclipse.swt.SWT.error(SWT.java:4168) at
org.eclipse.swt.widgets.Display.error(Display.java:1065) at
org.eclipse.swt.widgets.Display.createDisplay(Display.java:822) at
org.eclipse.swt.widgets.Display.create(Display.java:805) at
org.eclipse.swt.graphics.Device.(Device.java:130) at
org.eclipse.swt.widgets.Display.(Display.java:696) at
org.eclipse.swt.widgets.Display.(Display.java:687) at
com.samples.swt.first.Main.main(Main.java:8)
据我所知,我正在做一切正常.为什么我得到这个错误?它表示Display必须在主线程上创建,并且据我所知,它正在主线程上创建.然后继续谈论线程“主”中的异常…
编辑:
现在,错误已经消失,我从使用swt-debug.jar切换到swt.jar.如果有人知道为什么调试瓶子会导致这个错误我很想知道…