在公司整天搞b/s的东西难免有些乏味,业余时间搞搞cs程序,以前一直用awt/swing,不过感觉有点慢,虽然都是写些小玩意但是仍然不爽,于是转向swt了 ,刚买的thinking java中正好有一章介绍swt,不错,发个hello world。
import
org.eclipse.swt.widgets.
*
;
public class HelloSWT {
public static void main(String [] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText( " Hi there, SWT! " ); // Title bar
shell.open();
while ( ! shell.isDisposed())
if ( ! display.readAndDispatch())
display.sleep();
display.dispose();
}
}
public class HelloSWT {
public static void main(String [] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText( " Hi there, SWT! " ); // Title bar
shell.open();
while ( ! shell.isDisposed())
if ( ! display.readAndDispatch())
display.sleep();
display.dispose();
}
}
shell是窗体,shell.open()显示窗体。display管理swt和底层操作系统之间的连接。
如果事件队列中存在更多的事件在等待处理,那么readAndDispatch()方法将返回true,
swt必须显示释放资源dispose()因为绝大多数都是来自底层的系统资源。
swt不同与swing的是默认main线程处理ui,默认只有一个线程而不是两个线程。