原文:http://blogs.sun.com/Swing/entry/awt_swt_swing_java_gui3
作者:williamchen
譯者:Matthew Chen
備注:翻譯好累,但是第4篇是最有料的,我又不得不用紅字寫一些看法了。
Pros & Cons
To conclude the article, I'll give a list of pros & cons for each of them from a technical perspective.
我從一個技術層面給出了他們優劣勢上的一個清單,以結束本文。
AWT
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.
更少的內存。它對運行在有限環境中的GUI程序的開發,是合適的。
-
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.
-
More responsive, Because native components are rendered by operating systems.
-
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.
-
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:
然而,事物都有它們不好的一面。讓我們來例數它吧。
-
Less component types. Table and tree are very important components missing. while table and tree are very commonly used components in desktop applications.
-
Lacking rich component feature. Its buttons cannot have icon attached. This is clearly due to the fact, it is design according to principle.
-
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.
-
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
SWT has the following advantages:
SWT有如下優勢:
-
Rich component types. SWT provide a wide variety of components, from primitive components such as Buttons and Labels to Tables and Trees.
-
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.
-
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.
-
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:
不足之處:
-
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.
-
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.
-
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.
-
No Look And Feel support. The same reason as those of AWT.
-
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事件并添加自定義繪圖到組件上,但鑒于你只能控制繪制處理的一部分,這是十分有限的。你也只能在操作系統繪制完組件后補充,這并不能很好支持自定義。許多應用程序在自定義行為上有很高的要求。
[并不是所以的非Windows平臺都是美觀的mac或ubuntu,薄弱的操作系統圖形支持在大的范圍內是及其普遍的。]
Swing
Swing is the most powerful GUI toolkits. It has many advantages as well as disadvantages over the other two.
Swing是三者中最強大的GUI工具集。它和另外兩者相比同樣有自己的優劣勢。
-
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了]
-
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更多的功能。
-
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更面向對象,也更靈活而可擴展。
-
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 。
-
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中的標準庫。因此,你不用單獨地將它們隨你的應用程序一起分發。它們是平臺無關的,所以你不用擔心平臺兼容性。
-
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在每個平臺上都有相同的性能,不會有明顯的性能差異。
-
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或拖放機制開發高級的組件,例如浮動的固定組件,自定義彈出窗口,自定義菜單等。[這是一種好的機制,但你不能指望別人永遠無法超越你。]
-
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.
一個GUI的速度總是從兩個方面被衡量:響應時間和數據反饋時間。
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.
Swing的另一個優勢是它有許多的組件模型以提高輸送的性能。例如TableModel被映射為兩個維度上的數組。這樣,在Swing組件中甚至不需要進行數據方式的轉換。Swing表直接將應用程序數據顯示在屏幕上,節省了在數據轉換上所花費的事件。
As to Swing's disadvantages:
Swing也有不足之處:
-
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,也算一個變通吧]
-
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.上述的比較總的來說是技術上的總結。相對其他方面的因素也會影響你對一個工具集的選擇。例如,文檔,支持,學習曲線和社區等,但既然我們關注的是技術層面,就不在這里講的太多了。
(End)