import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class SWTHello {
public static void main(String[] args) {
/*
* Display的實例用于管理SWT與底層操作系統的連接,其
* 最重要的功能是根據平臺的事件處理模型實現SWT的event
* loop,一般來說,只要一個Display的實例就可以了。
* 注意,在創建任何window前(Shell實例)需創建Display實例,
* 在Shell實例關閉時除掉Display實例
*/
Display display = new Display();
/*
*Shell是作為主窗口
*/
Shell shell = new Shell(display);
/*
* SWT.NONE是Sytle bit,用于表明widget的style
*/
Label label = new Label(shell,SWT.NONE);
label.setText("Hello");
shell.pack();
label.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
shell.dispose();
}
}
關于Resource的Disposal
1、如果你用構造函數創建了widget或者graphic對象,當你不需要時你必須手動地dispose掉它;
2、如果你不是使用構造函數得到這個widget或者graphic對象,由于不是你allocate的,你不需要手動來dispose掉它;
3、如果你傳遞一個widget或者graphic對象的reference給另一個對象,那么你必須小心,不要在它仍在被使用中就dispose掉它;
4、當你close掉一個shell,那么這個shell及其子widget會被遞歸dispose掉的,雖然你不需再dispose掉那些widget,但是你必須free掉與這些widget相關的圖像資源;
5、如果在一個widget的生命期中創建了graphic對象,可以通過注冊一個dispose listener來free這個graphic對象,不過數據對象如Rectangle和Point沒有使用操作系統資源,不用手動dispose(它們也沒有dispose方法).