创建窗口时出错:无法启动类型外壳

问题描述

我对 Eclipse 的问题是,当我更改任何内容时,总是出现错误,我已将其更改为不同的内容,但最终我放弃了。

package MainWindowPackage;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.display;
import org.eclipse.swt.widgets.Shell;

public class SWTApplicationLoad {
    

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Object display;
        display display = display.getDefault();
        Shell shell2 = new Shell();
        
        shell2.setSize(450,300);
        shell2.setText("SWT Application");

        shell2.open();
        shell2.layout();
        while (!shell2.isdisposed()) {
            if (!display.readAnddispatch()) {
                display.sleep();
            }
        }
        
    }

}

每次对此进行更改时,我都会收到错误消息,我该怎么办?这是针对 Eclipse 的。

解决方法

您应该使用 new Display() 创建显示并在最后处理它。将 Shell 构造函数与 display 参数和样式一起使用也是正常的:

public static void main(final String[] args) {

  Display display = new Display();
  try {
      Shell shell2 = new Shell(display,SWT.SHELL_TRIM);

      shell2.setSize(450,300);
      shell2.setText("SWT Application");

      shell2.open();
      shell2.layout();

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