<p>from skyhit的blog: <a href="http://www.matrix.org.cn/blog/skyhit">http://www.matrix.org.cn/blog/skyhit</a></p>
<h3 class=title>swt代码研究(2)</h3>
<p>import org.eclipse.swt.*;<br>import org.eclipse.swt.widgets.*;<br>public class swthello {<br>public static void main(string[] args) {<br>/*<br>* display的实例用于管理swt与底层操作系统的连接,其<br>* 最重要的功能是根据平台的事件处理模型实现swt的event<br>* loop,一般来说,只要一个display的实例就可以了。<br>* 注意,在创建任何window前(shell实例)需创建display实例,<br>* 在shell实例关闭时除掉display实例<br>*/<br>display display = new display();<br></p>
<p><a name=more></a> </p>
<p>/*<br>*shell是作为主窗口 <br>*/<br>shell shell = new shell(display);<br>/*<br>* swt.none是sytle bit,用于表明widget的style<br>*/<br>label label = new label(shell,swt.none);<br>label.settext("hello");<br>shell.pack();<br>label.pack();<br>shell.open();<br>while(!shell.isdisposed())<br>{<br>if(!display.readanddispatch())<br>display.sleep();<br>}<br>shell.dispose();<br>}<br>}</p>
<p>关于resource的disposal<br>1、如果你用构造函数创建了widget或者graphic对象,当你不需要时你必须手动地dispose掉它;<br>2、如果你不是使用构造函数得到这个widget或者graphic对象,由于不是你allocate的,你不需要手动来dispose掉它;<br>3、如果你传递一个widget或者graphic对象的reference给另一个对象,那么你必须小心,不要在它仍在被使用中就dispose掉它;<br>4、当你close掉一个shell,那么这个shell及其子widget会被递归dispose掉的,虽然你不需再dispose掉那些widget,但是你必须free掉与这些widget相关的图像资源;<br>5、如果在一个widget的生命期中创建了graphic对象,可以通过注册一个dispose listener来free这个graphic对象,不过数据对象如rectangle和point没有使用操作系统资源,不用手动dispose(它们也没有dispose方法).<br></p>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器