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方法).
闽公网安备 35060202000074号