在Eclipse运行SWT程序没有错误,但不显示结果

package test;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;public class Hello {
public static void main(String[] args) { Display display=new Display(); Shell shell=new Shell(display); shell.setText("hello"); shell.setSize(400,300); Text helloText=new Text(shell,SWT.CENTER); helloText.setText("Hello World!"); helloText.pack(); shell.pack(); shell.open(); }}

● 如果安装SWT Designer后直接建立designer的SWT/JFace Java project,SWT Designer应该会自动帮你配置所有需要的reference库,其中就包括了org.eclipse.swt.win32.win32.x86_*.jar

● 如果是建立Eclipse plugin项目,有一点需要注意
输入完项目名后的下一个页面一定要选中generate an activator...选项和This plugin will make contributions to the UI选项,未选中generate an activator...选项,Eclipse不会帮你解析任何插件引用,也就是你的plugin dependence下不会有任何可以选择的插件。 未选中This plugin will make contributions to the UI,Eclipse不会帮你加入org.eclipse.swt.win32.win32.x86_*.jar的引用。

● 如果建立插件项目时,未选择那两个选项,没关系。
双击项目中的META-INF/MANIFEST.MF,或者plugin.xml,打开PDE环境,选择dependencies选项卡,在Required Plug-ins中点击Add...,添加如下两个插件:
org.eclipse.ui
org.eclipse.core.runtime
这样就会自动帮你添加org.eclipse.swt和org.eclipse.swt.win32.win32.x86_*.jar的引
用了。
● 之所以在Plugins下看不见org.eclipse.swt.win32.win32.x86_*.jar,是因为Eclipse只在那里显示具有plugin.xml文件的插件,通过winrar打开org.eclipse.swt.win32.win32.x86_*.jar,你会发现里面没有plugin.xml文件,只有META-INF/MANIFEST.MF。这可能就是传说中的纯资源插件,我也不是很清楚,呵呵。
通过这样配置后,在Eclipse3.3下运行为Java application试试,一切就正常,SWT窗口又打开了。
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-06-28
在shell.open()后面加上
// 控制窗口不关闭,如果没有下面的代码,窗口会立即自动关闭
while (!shell.isDisposed()) {
// 监听用户所引发的事件
if (!display.readAndDispatch()) {
// 让窗口处于睡眠状态
display.sleep();
}
}
// 当用户关闭窗口时,释放display占用的内存资源
display.dispose();本回答被提问者和网友采纳
第2个回答  2012-06-27
设置窗口显示了么?类似于setVisible(true);
相似回答