【问题标题】:Why my SWT application does not work?为什么我的 SWT 应用程序不起作用?
【发布时间】:2014-10-18 02:56:16
【问题描述】:

我正在尝试使用 Eclipse 创建一个 Hello World SWT 应用程序。我按照所有说明进行操作,最后我的程序无法运行。

这是我的代码:

import gnu.gcj.xlib.Display;
import org.eclipse.swt.widgets.Shell;


public class HelloWorldSWT {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello world!");
        shell.open();
        while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

}

这是我的错误信息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The constructor Shell(Display) is undefined
    The method readAndDispatch() is undefined for the type Display
    The method sleep() is undefined for the type Display
    The method dispose() is undefined for the type Display

   at HelloWorldSWT.main(HelloWorldSWT.java:13)

有人知道我如何检查问题吗?

【问题讨论】:

    标签: java eclipse swt


    【解决方案1】:

    我认为您导入了错误的 Display 类。正确的应该是

    org.eclipse.swt.widgets.Display
    

    【讨论】:

    • 当我替换“import org.eclipse.swt.widgets.Shell;”通过“org.eclipse.swt.widgets.Display”,Eclipse 告诉我我有一些冲突。我已经删除了我的代码的第一行(import gnu.gcj.xlib.Display;)。然后,当我尝试运行我的程序时,我仍然有一些错误消息:线程“main”中的异常 java.lang.Error:未解决的编译问题:Shell 无法解析为类型 Shell 无法解析为 HelloWorldSWT.main 中的类型( HelloWorldSWT.java:12)
    • 你必须让导入到org.eclipse.swt.widgets.Shell并添加导入org.eclipse.swt.widgets.Display
    【解决方案2】:

    清理文件夹中的所有内容,再次执行,导入 swt create proj,检查构建路径添加类,运行 这应该有效。 如果没有,请右键单击,单击清理,单击源...组织导入,再次运行。如果没有错误,应该可以工作

    swt.dll

    在你的库中,复制所有

    swt.dll

    到您的库路径。现在应该可以工作了。

    【讨论】:

      【解决方案3】:

      我犯了同样的错误。我的问题在第二步:我选择导入“org.eclipse.swt”。相反,您必须为您的操作系统选择正确的操作系统。就我而言,这是“org.eclipse.swt.win32.win32.x86”。完成此操作后,本教程中的其余步骤将按预期进行。

      上面的答案虽然正确,但可能比完成本教程的大多数人拥有更多的知识。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多