Picses' sky

Picses' sky
posts - 43, comments - 29, trackbacks - 0, articles - 24

AWT, SWT, Swing: Java GUI Clean Up (4)[翻]

Posted on 2007-11-29 08:22 Matthew Chen 阅读(1415) 评论(4)  编辑  收藏 所属分类: Java SE



译者:Matthew Chen


Pros & Cons

To conclude the article, I'll give a list of pros & cons for each of them from a technical perspective.



AWT is a deprecated toolkit by Sun. However, it does has advantage. In many areas other than desktop environment such as mobile or embedded device, AWT has many advantages.

AWT是Sun不推荐使用的工具集。然而它在许多非桌面环境如移动或嵌入式设备中有着自己的优势。[SWT因相同的理由在mobile环境中也具有优势,那Android呢。Java是世界上用得最广泛的编程语言,然而分裂和变异是生命体进化的必然,java guys,你准备好面对即将到来的一切了吗?]

Less memory. Therefore it is suitable for developing GUI application which runs in an limited environment, for example, mobile phone and so on.


  1. Less startup time. Because AWT components are native components which are implemented by the operating system, most of the binary codes can be loaded in advanced, for example, when operating system starts. This can help to reduce AWT application startup time.

  2. More responsive, Because native components are rendered by operating systems.

  3. Standard GUI toolkit supported by JRE since java 1.x time. You don't have to install them separately. You don't have to worry about platform differences.

  4. Mature and stable. Seldom will it crash you applications and it just works.

     1.  更少的启动事件。由于AWT组件是本地由操作系统实现的。绝大多数的二进制代码已经在如系统启动的时候被预装载了,这降低了它的启动事件。

     2.  更好的响应。由于本地组件由操作系统渲染。

     3.  从java 1.x时代就为JRE支持的标准GUI工具集,你不用单独安装它,你不用担心平台差异的问题。

     4.  成熟稳定的。它能够正常工作并很少使你的程序崩溃。

However, everything has its own Achilles heel. Let's enumerate them:


  1. Less component types. Table and tree are very important components missing. while table and tree are very commonly used components in desktop applications.

  2. Lacking rich component feature. Its buttons cannot have icon attached. This is clearly due to the fact, it is design according to principle.

  3. No Look And Feel support. AWT is designed to use native components. Therefore, it depends on the system to provide Look And Feel support. If there are no such support on the target platform, there's no way for AWT to change its look and feel.

  4. Not extensible. AWT components are native components. The JVM instance of AWT classes are in fact only references to the native components. The only extension point of AWT is its Canvas component, where you can create a custom component from scratch. However there's no way to extends and reuse an existing AWT component.

    1.   更少的组件类型。表和树这些重要的组件缺失了。它们是桌面应用程序中普遍使用的。

    2.   缺乏丰富的组件特征。按钮不支持图片附着。这很明显是它遵循的设计原则造成的。

    3.   不支持Look And Feel。AWT被设计为使用本地组件。因此,它依赖系统来提供Look And Feel支持。如果目标系统并不支持这一特性,那么AWT将无法改变它的Look And Feel。

    4.   无扩展性。AWT的组件是本地组件。JVM中的AWT类实例实际只是包含本地组件的引用。唯一的扩展点是AWT的Canvas组件,你可以从零开始创建自定义组件。然而无法继承和重用一个已有的AWT组件。[SWT同样,extends在这里的使用确实不如Swing可以做到的事情多,但别忘了原生只是人们加给SWT的一个名字而已,它的姓是IBM]


SWT has the following advantages:


  1. Rich component types. SWT provide a wide variety of components, from primitive components such as Buttons and Labels to Tables and Trees.

  2. Relative rich component feature. Although SWT is also designed following LCD principle, it has been re-engineered to support more component features by emulations. So compared to AWT, it has relative more component features.

  3. Better response time. Due to the same reason as AWT, SWT components are wrapped native components, which are rendered by the operating system. Operating system can usually optimize the rendering process. It often store the GUI binaries as shared library, which reduces memory footprint, therefore improve the response performance.

  4. Less memory consumption. It is easy to understand this point, since operating system can take care of the native components for applications.

    1.   丰富的组件类型。SWT提供了种类繁多的组件,从基础组件如按钮和标签到高级的表格和树。

    2.   相对的丰富组件特性。尽管SWT也遵循最大公倍数原则,它可以采用模拟的方式重新设计了对更多组件特性的支持。所以同AWT相比,它有着相对丰富的组件特性。

    3.   更快的响应时间。基于和AWT同样的原因,SWT组件包装了本地组件,由操作系统实现渲染。操作系统通常对渲染处理做了优化,保存GUI二进制代码为标准库,减少了内存的使用,提高了响应性能。

    4.    更少的内存消耗。既然操作系统为本地组件提供了优化,这一点就容易理解了。


As to disadvantages:


  1. Not a standard library shipped by JRE. Therefore you have to bundle them together with your applications. Also you have to build separate installers for every operating systems which you are to support.

  2. Immature and unstable. SWT due to its many design defect, for example resource management, and Windows-friendly design, is said to be unstable. It can perform very well on Windows. But on non-Windows operating systems, it is unstable and crashes very often. This is largely due to the fact that SWT leaves the resource management task to developers. However, not all developers can handle resource management correctly.

  3. Bad performance on non-Windows. As it is mentioned in No. 2, SWT is designed to accommodate Windows API, which results in bad performance on non-Windows platforms. The end results include slow performance, bad UI feelings, unstable or even memory leaks.

  4. No Look And Feel support. The same reason as those of AWT.

  5. Not extensible. The same reason as those AWT. In SWT, you can extend the component in a limited manner. For example, you can listen to the PAINT event and add custom drawing on the component. But it is very limited, since you can only control part of drawing process. You can only make up after the operating system draws the component. This is very bad for customization. Many modern applications needs such ability to provide customized behavior.

    1.   不在JRE的标准库中。因此你必须将它和你的程序捆绑在一起,并为你所要支持的每个操作系统创建单独的安装程序。

    2.   不够成熟和稳定。SWT因其设计上的一些缺陷,如资源管理,Windows友好等,被认为是不稳定的。它可以在Windows上表现得很好,但在其他操作系统上,它经常是不稳定且容易崩溃的。这很大程度上是因为它把资源管理交给开发者来处理,而并不是所有的开发人员能够正确地处理这些。

    3.   在非Windows平台下的性能不高。如同第2点提到的,SWT被设计为与Windows API相协调的,这导致了在非Windows平台上的性能问题,糟糕的UI感官,不稳定甚至内存泄露。

    4.   无Look And Feel 支持。和AWT同样的原因。

    5.    不可扩展,和AWT同样的原因。在SWT中你可以通过有限的方式扩展一个组件,例如,监听PAINT事件并添加自定义绘图到组件上,但鉴于你只能控制绘制处理的一部分,这是十分有限的。你也只能在操作系统绘制完组件后补充,这并不能很好支持自定义。许多应用程序在自定义行为上有很高的要求。



Swing is the most powerful GUI toolkits. It has many advantages as well as disadvantages over the other two.


  1. Rich component types. Swing provide very wide variety of standard components. These components are as rich as those SWT. Besides these standard components, Swing has a lots of third part components, due to its extensible nature. There are many commercial and open source swing libraries on market after years of development. 丰富的组件类型。Swing提供了非常广泛的标准组件。这些组件和SWT一样丰富。基于它良好的可扩展性,除了标准组件,Swing还提供了大量的第三方组件。许多商业或开源的Swing组件库在开发多年后都已经可以方便地获取了。[标准库中确实谈不上全面,但第三方组件的加入绝对媲美SWT了]

  2. Rich component features. Not only swing combines all the feature on every platform, it can even provide additional feature according to the platform the applications are running on. Swing component features are designed following principle. And also swing components are very easy to be extended, therefore, swing components can usually provide much more functionalities than its SWT and AWT counterparts.丰富的组件特性。Swing不仅包含了所有平台上的特性,它还支持根据程序所运行的平台来添加额外特性。Swing组件特性遵循特定原则,易于扩展,因此能够提供较SWT和AWT更多的功能。

  3. Good component API and model support. Swing is designed following MVC pattern, which is proved to be a very successful design pattern. Its API are mature and well designed. After years of evolution, Swing component APIs are becoming more and more powerful, flexible and extensible. Its API design is considered to be one of the most successful GUI API. They are more Object-Oriented, more flexible and more extensible compared to SWT and AWT. 好的组件API好模型支持。Swing遵循MVC模式,这是一种非常成功的设计模式。它的API成熟并设计良好。经过多年的演化,Swing组件APIs变得越来越强大,灵活和可扩展。它的API设计被认为是最成功的GUI API之一。较之SWT和AWT更面向对象,也更灵活而可扩展。

  4. Excellent Look And Feel support. MVC design model enables Swing the ability to separate component view from its data model. It has superior UI delegation which delegate UI rendering to UI classes. These classes are registered in an object, which represents a specific Look And Feel. There are now hundreds of LookAndFeel libraries available, which provides all kinds of different flavors of GUI styles. You can even write your own LookAndFeel based on others work. 出色的Look And Feel支持。MVC设计模型允许Swing分离组件试图和它的数据模型。它有高级的UI委托来将UI渲染委托给UI类。这些类被注册到一个展现特定的Look And Feel的对象上。已经有上百个Look And Feel 可以提高各种各样的GUI风格。你甚至可以基于其他人的成果编写组件的Look And Feel 。

  5. Standard GUI library. Swing as well as AWT is a standard library of JRE. Therefore, you don't have to ship them separately with your application. You don't have to worry about platform incompatibility since they are platform independent.标准的GUI库。Swing和AWT一样是JRE中的标准库。因此,你不用单独地将它们随你的应用程序一起分发。它们是平台无关的,所以你不用担心平台兼容性。

  6. Stable and mature. Swing has been developed for more than 7 years. It is becoming more and more mature and stable especially after Java 5. Since it is purely implemented in java, therefore, it has not such compatibility issues as those of SWT. Swing performs equally on every platforms. There are no drastically performance difference between them.成熟稳定。Swing已经开发出来7年之久了。在Java5之后它变得越来越成熟稳定。由于它是纯Java实现的,不会有SWT的兼容性问题。Swing在每个平台上都有相同的性能,不会有明显的性能差异。

  7. Extensible and flexible. Swing is a library entirely implemented in Java space. Therefore, it controls everything it needs. The architecture of Swing conforms to MVC pattern which exerts many of the advantage of Java as an Object-Oriented languages. In fact, it provides several ways to extend the components. Let me briefly enumerate them. A. Extend existing components. B. Extending by composite components. C. Writing custom components using Jcomponent from scratch. D. Using renderer and editor mechanism to extend complex swing components such as Jlist, JcomboBox, Jtable, Jtree etc. E. Creating a new LookAndFeel either by extending existing LookAndFeel or writing a new one from scratch. F. Using LayeredPane, GlassPane or Drag and Drop mechanism to develop advanced components, such as docking components, custom popup windows, custom menus etc. 可扩展和灵活性。Swing完全在Java空间中实现。它可以控制它所需要的一起。Swing基于MVC的结构使得它可以发挥Java作为一门面向对象语言的优势。它提供了许多扩展组件的方法。让我们来列举一下:A.继承已有组件;B.靠复合组件的方式扩展。C.从零开始使用JComponent编写自定义组件;D.使用渲染器和编辑器机制扩展复制的Swing组件,如JList,JComboBox,JTable,JTree等;E.基于已有Look And Feel 或从零开始创建新的Look And Feel;F.使用LayeredPane,GlassPane或拖放机制开发高级的组件,例如浮动的固定组件,自定义弹出窗口,自定义菜单等。[这是一种好的机制,但你不能指望别人永远无法超越你。]

  8. Good overall performance. Swing has ever been mocked for its speed. However, with the development of JRE, Swing is performing better and better now. Especially after Java 5, Swing's overall speed can compete with any native widget systems. 总体上良好的性能。Swing的速度是其为人诟病的一点。然而随着JRE的开发,Swing的性能如今已经有了很大的提高。特别是Java5之后,Swing的总体速度能够接近本地小控件系统。

A GUI speed can usually be measured in two aspects. One is response time, and the other is data feeding time.


Response time means the period between the time an event takes place and the time the component updates its UI. For example pressing a button, dragging a component, changing a tabbed pane selection, etc. In this aspect, native components usually have better response time than an emulated one. AWT and SWT components usually have better response time than those of Swings. However this is not always true. It depends on the operating systems. If native components are not implemented well on that platform, it might be the contrary. For example, Windows usually have their GUI library polished very well, while Linux platforms usually don't do well. Therefore, SWT can perform better than Swing on Windows, but might perform worse than Swing on Linux. In one word, the performance of AWT/SWT depends the underlying platforms. With the development of JRE, Swing response performance has been greatly enhanced due to JVM optimization, better swing implementation, graphics hardware acceleration etc. Swing in Java 6 can even compete with SWT on Windows. On non-Windows, Swing has much better response time than SWT.响应事件指从事件任务出现到组件更新UI的这段时间。例如按下一个按钮,拖动一个组件,改变一个多标签面板等。在这个方面本地组件总能比模拟组件有更好的响应。AWT和SWT通常比Swing表现出更好的响应时间。然而事实并非总是如此。这取决于操作系统。如果本地组件没有被良好的实现,那结果就是相反的。例如Windows开发了不错的GUI库,而Linux平台通常差得较远。SWT可能在Windows上表现得比Swing好,而在Linux上表现得比Swing差。也就是说,AWT/SWT的性能决定于底层平台。随着JRE的开发,Swing的响应性能能够随着JVM的优化,更好的实现方式,以及图形硬件加速而得到长足的改进。在Windows上,Java6的Swing可以媲美SWT的性能。在非Windows环境中,Swing可以表现出更好的响应时间。

Data feeding time means the time that is taken to transmit application data to UI components. For example, a student management software is asked to load some students information from the database and display them on a table. The time spent on transmitting those students from the memory to the table components is called feeding time. In this aspect, Swing usually performs better than the other two. It is not so obvious when there are not much data to be fed. But if there are tremendous data to be fed into the table, it is becoming more and more noticeable. Why? To understand it, you have to be reminded that JVM and native operating systems are two separate running environment. JNI call made by java program usually consumes much more time than ordinary java call. It is obvious that JNI call is an action that spans two different systems. When a JNI invocation happens, it usually involves two process. One is that Java data structure is translated into native system structure. And second, when a method returns, the returned values should also be translated into java objects. Not to mention other performance costs. When a large bound array of data are fed to native component, there will be large amount of JNI call which can drastically slow down the performance. 数据输送时间是指用于将应用程序数据传递给UI组件所需要的时间。例如,一个学生管理系统要求从数据库中装载学生信息并在一个表格中显示出来。花费在从内存到表格组件的数据传递时间被称为数据输送时间.在这个方面,Swing通常比其他二者的性能更好。或许当数据量不大的情况下并不明显。但当海量的数据被输送给表格的时候,这一点就显而易见了。为了理解这一点,提醒你注意JVM和本地操作系统是两个分离的运行时环境。由于JNI的调用在两个环境中跨越式地发生,通常比一个普通的Java调用花费更长的时间。通常这包含两个处理。一个是Java数据结构转换为本地数据结构,另一个是方法返回时的本地数据结构转换为Java对象。其他的性能开销暂时忽略不计。当一个大范围数组的数据从本地组件中输送过来,大量反复的JNI调用将极大地拖垮性能。[记起了blogjava上阿南的那个问题,或许,正是这个原因吧,数组也是对象,所以转换也不会是简单的]

And anther swing advantage is that it has many component models which can speed up the feeding performance greatly. For example tablemodel can be mapped to a two dimension array. In such case, there is even no data transmission happened in Swing components. Swing table directly displays the application data to the screen, greatly eliminating the time spent on data transmission.


    As to Swing's disadvantages:


  1. More memory consumption than AWT and SWT. Swing has all its components implemented by itself. Therefore, it has much more classes to be loaded in runtime. There are some other issues such as small mutable object creation, such as Rectangle, Point etc, these objects usually cannot be re-used due to synchronization consideration. Java creates all the objects on the heap. And small objects can often waste heap space. These small objects garbages can not be collected efficiently compared to large objects. Therefore, Swing application often creates a lots of small objects which can not be collected in time. More often it can result in performance degradation.比AWT和SWT更多的内存消耗。Swing自己实现了所有组件。因此,它在运行时装载了大量的类。一些其他的问题来源于小的可变对象的创建,如Rectangle,Point,这些对象基于同步的考虑通常不可重用。Java在堆上创建所以对象。小的对象通常导致了额外的堆空间消耗。许多小的对象较之大对象更难以有效地被垃圾回收。因此,Swing应用程序通常无法及时回收大而小的对象。这种情况的普遍就会导致性能下降。[通过研读一些gc方面的文章,你会对gc有更深入的了解,并懂得如何设计好的对象使用方式和生命周期。另外,component.getBounds(r)之类的方法不会自己创建新的Rectangle,也算一个变通吧]

  2. More application startup time. Now JVM is fast enough. Some people even said it can even compete with some C++ implementation. But still there are some java application which seem to be very slow. In fact, many java performance issues are caused by class loading. Class loading is an I/O operation which can greatly slow down java applications. Perhaps this is the issue that every dynamic linking system must face. Swing application usually involves thousands of thousand Swing classes. Before Swing application can show its main window, it has loaded much more classes than AWT and SWT applications. This can greatly increase Swing application startup time. If Swing classes are pre-loaded as shared system libraries, this issue can be resolved easily. 更多的启动时间。现在JVM已经快得多了。许多人甚至扬言它可以媲美C++的实现。但多数的Java应用程序还是看上去很慢。实际上,Java性能的很多问题来源于类装载机制。这是一个I/O操作,故而能够明显地降低Java应用程序的速度。也许这是每个动态链接系统中都要面对的问题吧。Swing通常包含了上千个Swing类的使用,在Swing应用程序可以显示它的主窗口之前,它比AWT或SWT装载了多得多的类。这严重降低了Swing的启动时间。这种问题也许会相对好一点如果Swing的类是以共享系统库的方式预加载的。[冷启动的问题吗?这种机制是操作系统的特性了,java可能以系统服务的方式出现吗?这个可要好好论证不能盲目做]

    The above comparison is mainly conducted in technical perspective. There are many other terms which should be considered when you choose a toolkit. For example, documentation, support, learning curves and community etc. But since we are focusing their technical sides, we shall not detail them here.上述的比较总的来说是技术上的总结。相对其他方面的因素也会影响你对一个工具集的选择。例如,文档,支持,学习曲线和社区等,但既然我们关注的是技术层面,就不在这里讲的太多了。



# re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回复  更多评论   

2007-11-29 09:43 by wanglin



# re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回复  更多评论   

2007-11-29 18:33 by gr8vyguy

# re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回复  更多评论   

2007-11-30 09:21 by 千里冰封

# re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回复  更多评论   

2007-12-04 10:22 by William Chen