private Button button = null; private Label label = null; public static void main(String[] args) { /* Before this is run, be sure to set up the following in the launch configuration * (Arguments->VM Arguments) for the correct SWT library path. * The following is a windows example: * -Djava.library.path="installation_directory/plugins/org.eclipse.swt.win32_3.0.0/os/win32/x86" */ Display display = Display.getDefault(); //Display负责与底层操作系统连接 SwtAppTest thisClass = new SwtAppTest(); thisClass.createSShell() ; thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {//循环等待请求 if (!display.readAndDispatch()) display.sleep ();//如果没有请示则处于Sleep状态 } display.dispose(); //千万别忘了把display dispose掉 }
/** * This method initializes sShell */ private void createSShell() { sShell = new Shell(); button = new Button(sShell, SWT.NONE); label = new Label(sShell, SWT.NONE); sShell.setSize(new Point(300,200)); sShell.setText("Shell"); button.setBounds(new org.eclipse.swt.graphics.Rectangle(116,116,72,22)); button.setText("Exit"); label.setText("Hello World!"); label.setBounds(new Rectangle(117,50, 72, 12)); button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { System.exit(0); } }); } }