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 閱讀(1434) 評論(4)  編輯  收藏 所屬分類: Java SE

          原文: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.

          我從一個技術(shù)層面給出了他們優(yōu)劣勢上的一個清單,以結(jié)束本文。

          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不推薦使用的工具集。然而它在許多非桌面環(huán)境如移動或嵌入式設(shè)備中有著自己的優(yōu)勢。[SWT因相同的理由在mobile環(huán)境中也具有優(yōu)勢,那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.

          更少的內(nèi)存。它對運行在有限環(huán)境中的GUI程序的開發(fā),是合適的。

          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組件是本地由操作系統(tǒng)實現(xiàn)的。絕大多數(shù)的二進制代碼已經(jīng)在如系統(tǒng)啟動的時候被預(yù)裝載了,這降低了它的啟動事件。

               2.  更好的響應(yīng)。由于本地組件由操作系統(tǒng)渲染。

               3.  從java 1.x時代就為JRE支持的標準GUI工具集,你不用單獨安裝它,你不用擔心平臺差異的問題。

               4.  成熟穩(wěn)定的。它能夠正常工作并很少使你的程序崩潰。

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

          然而,事物都有它們不好的一面。讓我們來例數(shù)它吧。

          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.   更少的組件類型。表和樹這些重要的組件缺失了。它們是桌面應(yīng)用程序中普遍使用的。

              2.   缺乏豐富的組件特征。按鈕不支持圖片附著。這很明顯是它遵循的設(shè)計原則造成的。

              3.   不支持Look And Feel。AWT被設(shè)計為使用本地組件。因此,它依賴系統(tǒng)來提供Look And Feel支持。如果目標系統(tǒng)并不支持這一特性,那么AWT將無法改變它的Look And Feel。

              4.   無擴展性。AWT的組件是本地組件。JVM中的AWT類實例實際只是包含本地組件的引用。唯一的擴展點是AWT的Canvas組件,你可以從零開始創(chuàng)建自定義組件。然而無法繼承和重用一個已有的AWT組件。[SWT同樣,extends在這里的使用確實不如Swing可以做到的事情多,但別忘了原生只是人們加給SWT的一個名字而已,它的姓是IBM]

          SWT

          SWT has the following advantages:

          SWT有如下優(yōu)勢:

          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提供了種類繁多的組件,從基礎(chǔ)組件如按鈕和標簽到高級的表格和樹。

              2.   相對的豐富組件特性。盡管SWT也遵循最大公倍數(shù)原則,它可以采用模擬的方式重新設(shè)計了對更多組件特性的支持。所以同AWT相比,它有著相對豐富的組件特性。

              3.   更快的響應(yīng)時間?;诤虯WT同樣的原因,SWT組件包裝了本地組件,由操作系統(tǒng)實現(xiàn)渲染。操作系統(tǒng)通常對渲染處理做了優(yōu)化,保存GUI二進制代碼為標準庫,減少了內(nèi)存的使用,提高了響應(yīng)性能。

              4.    更少的內(nèi)存消耗。既然操作系統(tǒng)為本地組件提供了優(yōu)化,這一點就容易理解了。

          [之前的文章說過組件類型和特征(有時也有特性)這兩個概念,三者分別基于兩類組件使用原則實現(xiàn),本地模擬比起真正的仿真模擬確實有不足之處,這種不足并不僅僅是效果上的缺失,見下面的數(shù)據(jù)輸送時間部分]

          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的標準庫中。因此你必須將它和你的程序捆綁在一起,并為你所要支持的每個操作系統(tǒng)創(chuàng)建單獨的安裝程序。

              2.   不夠成熟和穩(wěn)定。SWT因其設(shè)計上的一些缺陷,如資源管理,Windows友好等,被認為是不穩(wěn)定的。它可以在Windows上表現(xiàn)得很好,但在其他操作系統(tǒng)上,它經(jīng)常是不穩(wěn)定且容易崩潰的。這很大程度上是因為它把資源管理交給開發(fā)者來處理,而并不是所有的開發(fā)人員能夠正確地處理這些。

              3.   在非Windows平臺下的性能不高。如同第2點提到的,SWT被設(shè)計為與Windows API相協(xié)調(diào)的,這導(dǎo)致了在非Windows平臺上的性能問題,糟糕的UI感官,不穩(wěn)定甚至內(nèi)存泄露。

              4.   無Look And Feel 支持。和AWT同樣的原因。

              5.    不可擴展,和AWT同樣的原因。在SWT中你可以通過有限的方式擴展一個組件,例如,監(jiān)聽PAINT事件并添加自定義繪圖到組件上,但鑒于你只能控制繪制處理的一部分,這是十分有限的。你也只能在操作系統(tǒng)繪制完組件后補充,這并不能很好支持自定義。許多應(yīng)用程序在自定義行為上有很高的要求。

          [并不是所以的非Windows平臺都是美觀的mac或ubuntu,薄弱的操作系統(tǒng)圖形支持在大的范圍內(nèi)是及其普遍的。]

          Swing

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

          Swing是三者中最強大的GUI工具集。它和另外兩者相比同樣有自己的優(yōu)劣勢。

          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一樣豐富?;谒己玫目蓴U展性,除了標準組件,Swing還提供了大量的第三方組件。許多商業(yè)或開源的Swing組件庫在開發(fā)多年后都已經(jīng)可以方便地獲取了。[標準庫中確實談不上全面,但第三方組件的加入絕對媲美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不僅包含了所有平臺上的特性,它還支持根據(jù)程序所運行的平臺來添加額外特性。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模式,這是一種非常成功的設(shè)計模式。它的API成熟并設(shè)計良好。經(jīng)過多年的演化,Swing組件APIs變得越來越強大,靈活和可擴展。它的API設(shè)計被認為是最成功的GUI API之一。較之SWT和AWT更面向?qū)ο螅哺`活而可擴展。

          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設(shè)計模型允許Swing分離組件試圖和它的數(shù)據(jù)模型。它有高級的UI委托來將UI渲染委托給UI類。這些類被注冊到一個展現(xiàn)特定的Look And Feel的對象上。已經(jīng)有上百個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中的標準庫。因此,你不用單獨地將它們隨你的應(yīng)用程序一起分發(fā)。它們是平臺無關(guān)的,所以你不用擔心平臺兼容性。

          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.成熟穩(wěn)定。Swing已經(jīng)開發(fā)出來7年之久了。在Java5之后它變得越來越成熟穩(wěn)定。由于它是純Java實現(xiàn)的,不會有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空間中實現(xiàn)。它可以控制它所需要的一起。Swing基于MVC的結(jié)構(gòu)使得它可以發(fā)揮Java作為一門面向?qū)ο笳Z言的優(yōu)勢。它提供了許多擴展組件的方法。讓我們來列舉一下:A.繼承已有組件;B.靠復(fù)合組件的方式擴展。C.從零開始使用JComponent編寫自定義組件;D.使用渲染器和編輯器機制擴展復(fù)制的Swing組件,如JList,JComboBox,JTable,JTree等;E.基于已有Look And Feel 或從零開始創(chuàng)建新的Look And Feel;F.使用LayeredPane,GlassPane或拖放機制開發(fā)高級的組件,例如浮動的固定組件,自定義彈出窗口,自定義菜單等。[這是一種好的機制,但你不能指望別人永遠無法超越你。]

          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的開發(fā),Swing的性能如今已經(jīng)有了很大的提高。特別是Java5之后,Swing的總體速度能夠接近本地小控件系統(tǒng)。

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

          一個GUI的速度總是從兩個方面被衡量:響應(yīng)時間和數(shù)據(jù)反饋時間。

          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.響應(yīng)事件指從事件任務(wù)出現(xiàn)到組件更新UI的這段時間。例如按下一個按鈕,拖動一個組件,改變一個多標簽面板等。在這個方面本地組件總能比模擬組件有更好的響應(yīng)。AWT和SWT通常比Swing表現(xiàn)出更好的響應(yīng)時間。然而事實并非總是如此。這取決于操作系統(tǒng)。如果本地組件沒有被良好的實現(xiàn),那結(jié)果就是相反的。例如Windows開發(fā)了不錯的GUI庫,而Linux平臺通常差得較遠。SWT可能在Windows上表現(xiàn)得比Swing好,而在Linux上表現(xiàn)得比Swing差。也就是說,AWT/SWT的性能決定于底層平臺。隨著JRE的開發(fā),Swing的響應(yīng)性能能夠隨著JVM的優(yōu)化,更好的實現(xiàn)方式,以及圖形硬件加速而得到長足的改進。在Windows上,Java6的Swing可以媲美SWT的性能。在非Windows環(huán)境中,Swing可以表現(xiàn)出更好的響應(yīng)時間。

          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. 數(shù)據(jù)輸送時間是指用于將應(yīng)用程序數(shù)據(jù)傳遞給UI組件所需要的時間。例如,一個學生管理系統(tǒng)要求從數(shù)據(jù)庫中裝載學生信息并在一個表格中顯示出來?;ㄙM在從內(nèi)存到表格組件的數(shù)據(jù)傳遞時間被稱為數(shù)據(jù)輸送時間.在這個方面,Swing通常比其他二者的性能更好?;蛟S當數(shù)據(jù)量不大的情況下并不明顯。但當海量的數(shù)據(jù)被輸送給表格的時候,這一點就顯而易見了。為了理解這一點,提醒你注意JVM和本地操作系統(tǒng)是兩個分離的運行時環(huán)境。由于JNI的調(diào)用在兩個環(huán)境中跨越式地發(fā)生,通常比一個普通的Java調(diào)用花費更長的時間。通常這包含兩個處理。一個是Java數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為本地數(shù)據(jù)結(jié)構(gòu),另一個是方法返回時的本地數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為Java對象。其他的性能開銷暫時忽略不計。當一個大范圍數(shù)組的數(shù)據(jù)從本地組件中輸送過來,大量反復(fù)的JNI調(diào)用將極大地拖垮性能。[記起了blogjava上阿南的那個問題,或許,正是這個原因吧,數(shù)組也是對象,所以轉(zhuǎn)換也不會是簡單的]

          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的另一個優(yōu)勢是它有許多的組件模型以提高輸送的性能。例如TableModel被映射為兩個維度上的數(shù)組。這樣,在Swing組件中甚至不需要進行數(shù)據(jù)方式的轉(zhuǎn)換。Swing表直接將應(yīng)用程序數(shù)據(jù)顯示在屏幕上,節(jié)省了在數(shù)據(jù)轉(zhuǎn)換上所花費的事件。

            As to Swing's disadvantages:

            Swing也有不足之處:

          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更多的內(nèi)存消耗。Swing自己實現(xiàn)了所有組件。因此,它在運行時裝載了大量的類。一些其他的問題來源于小的可變對象的創(chuàng)建,如Rectangle,Point,這些對象基于同步的考慮通常不可重用。Java在堆上創(chuàng)建所以對象。小的對象通常導(dǎo)致了額外的堆空間消耗。許多小的對象較之大對象更難以有效地被垃圾回收。因此,Swing應(yīng)用程序通常無法及時回收大而小的對象。這種情況的普遍就會導(dǎo)致性能下降。[通過研讀一些gc方面的文章,你會對gc有更深入的了解,并懂得如何設(shè)計好的對象使用方式和生命周期。另外,component.getBounds(r)之類的方法不會自己創(chuàng)建新的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. 更多的啟動時間?,F(xiàn)在JVM已經(jīng)快得多了。許多人甚至揚言它可以媲美C++的實現(xiàn)。但多數(shù)的Java應(yīng)用程序還是看上去很慢。實際上,Java性能的很多問題來源于類裝載機制。這是一個I/O操作,故而能夠明顯地降低Java應(yīng)用程序的速度。也許這是每個動態(tài)鏈接系統(tǒng)中都要面對的問題吧。Swing通常包含了上千個Swing類的使用,在Swing應(yīng)用程序可以顯示它的主窗口之前,它比AWT或SWT裝載了多得多的類。這嚴重降低了Swing的啟動時間。這種問題也許會相對好一點如果Swing的類是以共享系統(tǒng)庫的方式預(yù)加載的。[冷啟動的問題嗎?這種機制是操作系統(tǒng)的特性了,java可能以系統(tǒng)服務(wù)的方式出現(xiàn)嗎?這個可要好好論證不能盲目做]

            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.上述的比較總的來說是技術(shù)上的總結(jié)。相對其他方面的因素也會影響你對一個工具集的選擇。例如,文檔,支持,學習曲線和社區(qū)等,但既然我們關(guān)注的是技術(shù)層面,就不在這里講的太多了。

          (End)

          Feedback

          # re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回復(fù)  更多評論   

          2007-11-29 09:43 by wanglin
          翻譯的非常不錯

          贊一個

          同樣我多swing有興趣,不過因為工作的緣故多數(shù)在html/ext/rcp之間周旋,希望在swing上各位同愛多多貢獻。解我之痛。

          # re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回復(fù)  更多評論   

          2007-11-29 18:33 by gr8vyguy
          很多觀點都過時了

          # re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回復(fù)  更多評論   

          2007-11-30 09:21 by 千里冰封
          不過,SWING確實是最強大的

          # re: AWT, SWT, Swing: Java GUI Clean Up (4)[翻]  回復(fù)  更多評論   

          2007-12-04 10:22 by William Chen
          謝謝作者的翻譯!
          我的觀點僅供大家參考。有一些問題我也沒有搞懂,尤其是SWT部分的,大家可以互相討論。原文是我花了一天寫的一篇文章,英文實在不太規(guī)范,但當時只是作為講稿的,沒有進行任何修改。有時間一定修改一下,只是最近一直很忙。請多諒解。

          主站蜘蛛池模板: 如皋市| 万源市| 胶南市| 博野县| 于田县| 铁力市| 巧家县| 贡觉县| 阿尔山市| 济宁市| 侯马市| 隆尧县| 抚远县| 文成县| 潍坊市| 宣城市| 庐江县| 福建省| 桃源县| 安达市| 陵川县| 石河子市| 阜新| 大竹县| 广丰县| 张家川| 曲靖市| 孟州市| 苗栗市| 湖南省| 墨江| 玉环县| 和田市| 大安市| 富宁县| 荃湾区| 桓台县| 原阳县| 兴仁县| 疏附县| 富川|