---
title: "SWT行,AWT/Swing也行系列(1)-实现半透明及不规则窗体"
author: "mmdev"
date: "2007-12-20T16:10:00Z"
tags: []
slug: "swt-awt-swing-1"
draft: false
lastmod: "2012-04-14T20:14:02Z"
id: 1486365
url: "https://mmdev.iteye.com/blog/1486365"
---

对于Java来说，GUI开发一直都是项比较头疼的事情。从AWT的功能奇缺，到Swing的臃肿不堪，往如梦魇般困扰着Java开发人员。

于是，有一群人开始走向了邪路……

这群误入岐途的人（至少Sun是这么认为的……），走出了自己的一条路，名为SWT（Standard Widget Toolkit）的不归路(Sun,Sun|||)。

说起SWT冒着被诟病为邪恶所换取的，无外以下几点：

1.彻底摒弃了AWT/Swing，某种意义上甚至架空了JVM，比如其通过dispose()即时释放资源。（当然，大家也都知道这意味着什么）

2.功能几乎全用本地系统完成，所以其界面与本地程序界面也高度一致，一改Java GUI的沉闷，令人眼前一亮。

3.支持本地API调用，也就是说无论本地系统能实现什么，都可以通过SWT照样实现出来。

4.使用"用户线程"作为唯一线程，只有在这个线程中才能调用对构件或某些图形API的访问操作，减少图形操作时线程错误。但也允许间接的在非用户线程的进行图形构件的访问操作。

5.在Windows下运行速度有保证，明显超越AWT/Swing，且较稳定。

6.目前已有较丰富的组件库，有JFace等辅助项目，借助于IDE之利，开发GUI程序速度N快……

但是，只所以被称为邪道，也不是空穴来风，它的缺陷也是显而易见的，比如：

1.JNI调用耗费的时间是不能忽略的。JNI调用速度要比普通Java方法调用慢好几倍甚至几十倍。即便是在Java 6中，这种情况并没有改善。且Swing绝大部分是用Java平台模拟出的组件，这个过程都在一个系统平台内完成。而SWT是部分在本地系统完成，部分在Java平台完成，进行操作时要在这两个平台之间需要进行频繁的数据交互。SWT并没有从根本上解决效率问题。

2.Swing可以享受JVM的特殊待遇，进行特殊优化，比如inline，JIT代码，Swing事件队列对于事件的预处理（合并Paint事件，批处理Java 2D光栅指令等），这就像本地组件可以利用操作系统进行优化一样。而SWT由于采用本地操作，无法完成。

3.SWT只能自顶向下地构建GUI。因此，如果没有父容器，子控件也就不存在，父容器无法在以后任意改变。不如AWT/Swing灵活。

4.在目前来讲，SWT还是一个有限的图形环境（Sun，依旧是Sun）。到目前为止，它对于Java2D和Java3D的支持还不怎么好。

5.SWT在Windows下速度虽快，是占了Microsoft提供的大量API之利，在其他平台上则持平或较慢于AWT/Swing。

6.与AWT/Swing不同，SWT和JFace并不是Java技术的标准配置，需要在将JAR文件放到Java CLASSPATH中，并将DLL文件放到系统PATH中才能运行，较AWT/Swing更繁琐，如果某天Sun大神发威，和标准JRE兼容都可能成为问题。

两种论调势成水火，各不相下，似乎都要彻底压到另一方才可罢休。

其实我认为大可不必，使用何种技术，大体上只有开发人员才会关心，技术外的那些才是用户所关心的。开发人员间再怎么争辩，其实在外人眼里都是无意义的窝里斗罢了。

比如，SWT可以包含AWT/Swing，而在AWT/Swing下，要实现SWT的功能也是轻而易举的。

我举几个程序的例子。

AWT/Swing与SWT透明窗体实现的比较:

在SWT中，由于高度集成本地环境,能够完成很多AWT/Swing力所不能及,很Cool的工作,比如半透明的窗体,代码如下:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.dll.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.SWT;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.graphics.Point;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.internal.win32.OS;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.internal.win32.TCHAR;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.layout.FillLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Display;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Shell;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassSWTTransTest...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramargs  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Displaydisplay=newDisplay();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Shellshell=newShell(display,SWT.CLOSE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setSize(newPoint(400,400));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setLayout(newFillLayout());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setText("SWT半透明窗体实现");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//打开shell  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.open();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设置窗体透明  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)OS.SetWindowLong(shell.handle,OS.GWL\_EXSTYLE,OS.GetWindowLong(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.handle,OS.GWL\_EXSTYLE)^0x80000);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//loadUser32.dlllib  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)TCHARlpLibFileName=newTCHAR(0,"User32.dll",true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inthInst=OS.LoadLibrary(lpLibFileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(hInst!=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定调用函数名称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringname="SetLayeredWindowAttributes�";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]lpProcName=newbyte\[name.length()];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;lpProcName.length;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)lpProcName\[i]=(byte)name.charAt(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//检索DLL输出函数地址  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intfun=OS.GetProcAddress(hInst,lpProcName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//当函数存在  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(fun!=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//150为透明度,在0-255之间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)OS.CallWindowProc(fun,shell.handle,0,150,2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//释放lib  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)OS.FreeLibrary(hInst);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(!shell.isDisposed())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(!display.readAndDispatch())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)display.sleep();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果如图:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/swta.gif)

嗯,很简单,很方便.

不过,似乎有个问题,这些反复出现的OS,不就是封装的Win32 API吗?那么,SWT由于依赖本地平台能够实现,那么Swing那种"画"出来的界面可以实现吗?答案是肯定的,事实上,没有一种界面不是系统"画"来的,只要能获得窗体的hWnd,也就是句柄,任何窗体的操作都是张飞吃豆芽-小菜一碟罢了.

下面,我用AWT/Swing完成同样的操作.

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.dll.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Frame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.WindowAdapter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.WindowEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JFrame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.loon.framework.dll.NativeLoader;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:AWT/SWing实现真窗体透明&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassTransTest...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(finalString\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//助于SetLayeredWindowAttributes函数,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//在Windows下,AWT/SWing要实现不规则窗体、半透明窗体是非常容易的  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JFrameframe=newJFrame("透明窗体测试");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Frameframe=newFrame("透明窗体测试");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//\*frame.addWindowListener(newWindowAdapter(){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidwindowClosing(WindowEventwe){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//NativeLoader为制作好的本地API集合，0.6f为透明度60%显示  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)NativeLoader.getInstance().setTransparence(frame,0.6f);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setSize(400,400);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setLocationRelativeTo(null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果图:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/loona.gif)

事实上,我们只要在NativeLoader中通过当前object在jawt.dll中的运行时hWnd,(即AWT窗体的hWnd,因为Swing底层是AWT,两者一至),与SWT是没有任何区别的.

再比如在AWT/Swing中,似乎很难实现真正的不规则窗体,充其量只能做一些"伪不规则窗体",而SWT借助本地支持却能轻易实现.

AWT/Swing与SWT不规则窗体实现的比较:

比如SWT实现不规则窗体.

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.dll.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.SWT;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.graphics.Image;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.graphics.ImageData;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.graphics.Point;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.graphics.Region;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Display;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Event;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Listener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.eclipse.swt.widgets.Shell;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassSWTWindowFrm...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateShellshell;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateDisplaydisplay=Display.getDefault();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateImageimage=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateImageDataimageData=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SWTWindowFrmwindow=newSWTWindowFrm();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)window.open();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidopen()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)createUI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.open();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.layout();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(!shell.isDisposed())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!display.readAndDispatch())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)display.sleep();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)protectedvoidcreateUI()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell=newShell(display,SWT.NO\_TRIM);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setSize(500,375);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setText("SWT实现不规则窗体");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)createShell();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)Listenerlistener=newListener()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intstartX,startY;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidhandleEvent(Evente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//注入鼠标事件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(e.type==SWT.MouseDown&&e.button==1)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)startX=e.x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)startY=e.y;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(e.type==SWT.MouseMove&&(e.stateMask&SWT.BUTTON1)!=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Pointp=shell.toDisplay(e.x,e.y);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p.x-=startX;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p.y-=startY;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setLocation(p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(e.type==SWT.Paint)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.gc.drawImage(image,imageData.x,imageData.y);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.addListener(SWT.KeyDown,listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.addListener(SWT.MouseDown,listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.addListener(SWT.MouseMove,listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.addListener(SWT.Paint,listener);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)protectedvoidcreateShell()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringpath=this.getClass().getResource("role.gif").getPath();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)image=newImage(display,newImageData(path));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Regionregion=newRegion();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)imageData=image.getImageData();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将255(白色)定为透明区域，镂空  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(imageData.alphaData!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inty=0;y&lt;imageData.height;y++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(intx=0;x&lt;imageData.width;x++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(imageData.getAlpha(x,y)==255)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)region.add(imageData.x+x,imageData.y+y,1,1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ImageDatamask=imageData.getTransparencyMask();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inty=0;y&lt;mask.height;y++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(intx=0;x&lt;mask.width;x++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(mask.getPixel(x,y)!=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)region.add(imageData.x+x,imageData.y+y,1,1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setRegion(region);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shell.setSize(imageData.x+imageData.width,imageData.y  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+imageData.height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果图如下:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/swtrole.gif)

但是,AWT/Swing做不到吗?我已经说过了,只要能得到句柄,就没有修改不了的窗体存在.而AWT/Swing显然是可以得到运行时本地句柄的.

下面来个AWT/Swing实现:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.dll.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Image;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Point;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Window;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.FocusEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.FocusListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.MouseEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.MouseMotionListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.image.BufferedImage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.imageio.ImageIO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importorg.loon.framework.dll.win32.UIWindow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//UIWindow为本人提供类，内部已设置255为镂空色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassMyJWindowextendsUIWindowimplementsMouseMotionListener,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)FocusListener...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PointmousePointer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicMyJWindow(Imageimg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super(img);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)init();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidinit()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)addMouseMotionListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)addFocusListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidfocusGained(FocusEventaFocusEvent)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PointaPoint=getLocation();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setLocation(15000,0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setLocation(aPoint);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidfocusLost(FocusEventaFocusEvent)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidmouseDragged(MouseEventaMouseEvent)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PointaPoint=aMouseEvent.getPoint();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intx=getX()+aPoint.x-mousePointer.x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inty=getY()+aPoint.y-mousePointer.y;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setLocation(x,y);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidmouseMoved(MouseEventaMouseEvent)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mousePointer=aMouseEvent.getPoint();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringpath=MyJWindow.class.getResource("role.gif").getPath();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedImagebackgroundImage=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)backgroundImage=ImageIO.read(newFile(path));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Windowwindow=newMyJWindow(backgroundImage);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)window.setBounds(0,0,backgroundImage.getWidth(null),backgroundImage  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).getHeight(null));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)window.setLocationRelativeTo(null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)window.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)window.validate();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果如下图:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/roleui0.gif)

两者有区别吗?

我们可以从此得知,不,可以说确信,在同样利用本地API时,SWT与AWT/Swing相比是没有太大优势的.而且Sun已经开始优化AWT对本地系统的支持,再不久的将来,SWT可能将没有任何优势可言了^^(我是亲Sun一派~)

其中org.loon.framework.dll包下载路径位于:[loonframework-dll](http://download.csdn.net/source/312917)