`
djsl6071
  • 浏览: 593443 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

AWT, SWT, Swing: Java GUI Clean Up (1)

阅读更多

Overview概述

Java GUI toolkits have always been a controversial topic. The same type of debates also happened in other languages such as Smalltalk. In fact there exist such debates on every platform-independent language. It is especially prominent in Java because Java is the most dominating language today.

Java GUI 工具包一直是一个倍受争议的话题。同样的争论也发生在其他编程语言如Smalltalk。实际上每个平台无关的语言都存在着这样的争论。Java作为当前最受广泛使用的编程语言而尤为突出。

So what’s the controversy? Basically, it is a dispute between people who support emulated components(or widgets, or controls) and those who support native components. There are two camps among Java developers, one of which advocates for emulated components as in Swing, and the other native components as in SWT.

这场争论在支持模拟组件(如widgets和control,在下文中也称之为仿造组件)和支持本地组件(在下文中也称之为原生组件)的人们之间展开,于是Java开发者形成了两个不同的阵营,提倡使用模拟组件的Swing,和提倡使用原生组件的SWT。[战争从来都在相似的人们之间展开,因为它们想要一个东西]

History历史

There are many stories about around the dispute on the Internet. You should probably have already heard of them. One of them can help you to understand the whole picture. Let’s start with this one, in which Amy Fowler from Swing team is one of the protagonists.

Internet上有许多围绕这一争论的故事。你可能已经听说过它们中的大多数了,其中之一有助于让你理清头绪,让我们就从这里开始,Amy Fowler是Swing阵营的一个倡导者。

Back in 1990s, there were three companies, which developed their products using Smalltalk. They are IBM, Digital Talk and Parc-Place. IBM and Digital Talk adopted native components, while Parc-Place believed that emulation is the way to go. At the very beginning, all things went well until IBM overtook the others. The other two then merged to form a new company named Objectshare. Then a huge battle erupted when they tried to merge their products into one. The native and emulated crowd fought the battle nearly to death. When Amy Fowler from Parc-place, who insisted on emulated components, finally won the victory, IBM had got all of their accounts, because the two companies did nothing but quarrel for an entire year. The share price of the company, went down to under 1 dollar a share and were pulled from NASDAQ because of incorrect financial reportings.

回到上个世纪90年代,曾几何时有3家庞大的Smalltalk公司——IBM、Parc-Place和 Digitalk。在90年代初期3家公司的市场份额大致相等,生活是美好的。Parc-Place采用仿窗口部件(emulated widgets)的设计(即Swing的设计),IBM和Digitalk则采用原生窗口部件(native widgets)。后来IBM压倒了另外两家,因此他们打算合并成一家,假设叫做Parc-Place Digitalk。随后当他们试图将他们的产品融合到一个叫做Jigsaw的计划中时爆发了一场大战,计划由于政治原因失败了(开发人员实际上已经能让它运转起来),就因为原生和仿造两派的死战。Amy赢得了精神上的胜利,不过IBM赢得了他们所有的生意,因为这两家公司在一整年里除了吵架什么都没做。当尘埃落定之后PPD(Parc-Place Digitalk当时已改名为Objectshare,跟Windscale改名为Sellafield的原因相同——让人们淡忘之前发生的灾难)的股票价格从60美元掉到了低于1美元1股。他们因为伪报收入被NASDAQ摘牌,从此消失。




At that time, AWT had just existed. Sun had built a basic set of portable control classes that mapped to native components on the different operating systems. However AWT was very buggy. It was beyond belief this was just poor code that could be fixed. Just then, Amy was hired by Sun, and she promised to solve all of the problems by doing a lightweight solution. Convinced, Sun management to make her the head of the GUI development. Amy then hired all her old Parc-Place friends and they set about creating Swing.

当时,AWT已经出现了。SUN当时已经建立了一套基本的可移植控件类,这些类映射到不同操作系统上的原生窗口组件(native widget),当时的AWT还满是漏洞,远不能称为可靠,还需要SUN的coder们去修补。然后Amy被雇佣了,她承诺通过轻量级方案解决所有窗口组件的问题,以此说服SUN管理层让她当了GUI开发部门的头头。随后Amy雇佣了所有她过去在Parc-Place的旧朋友,让他们来开发Swing。[不知道Amy是不是理想主义者,但我是,从我选择Java那天起]

In IBM, Visual Age for Java were first written in Smalltalk, which used native components. Then they started to migrate them to a Java code. All of the IBM developers are the those Smalltalk guy, and they hated Swing for its emulated nature. They reluctantly built it using Swing. At that time, it is of no doubt that Swing was ugly, slow and buggy. Therefore they created a project to migrate the Smalltalk native components over to Java. This toolkit was later called SWT, which initially means Simple Widget Toolkit and later Standard Widget Toolkit. It was a success as they released a product called Visual Age Micro Edition. The folks later found that there were bugs in Swing reading windows events, which could cause memory leaks. So they took the decision that SWT and AWT/Swing should not co-exist and then put the toolkit into Eclipse which is a tools platform derived from the early Visual Age.

在IBM,VisualAge for Java最初是用Smalltalk(用的是原生窗口组件)写的,当将这些工具向Java代码库迁移时,他们需要一套窗口组件。IBM这边的开发人员都是原来搞Smalltalk的那一批人,他们对管理层要求用Swing来构建WebSphere Studio工具都非常不情愿。“Swing是个可怕的充满缺陷的怪兽“。因此开始了一个新的项目,把他们的Smalltalk原生窗口组件移植到Java上去。这个工具集后来被成为SWT,S开始是Simple的缩写,不过后来变成了Standard的缩写。这个项目获得了成功,被运用在发布的 VisualAge Micro Edition产品中。他们当时发现在Swing读事件队列的时候用了一种可能留下内存漏洞的方式,而不得不采用他们自己的查询 Windows事件队列的循环,以纠正这个错误。这促成了他们关于SWT和 AWT/Swing不能共存的决定。他们把这个工具包放到了Eclipse中,这是一个来自于早期Visual Age的工具平台。





The above story should have given you an overview of the history of the three, especially SWT. Now you might think, the reason IBM did to create SWT was valid and Swing should follow the way SWT has been on. Actually this opinion is very superficial. When you dwell upon to the nature of Java, you will find that it is not that simple as you expect.

你应该已经从上述的故事中对三者的历史有了大概的了解,尤其是SWT。现在你也许会觉得,IBM创建SWT的理由是合理的而Swing应该沿用SWT采用的方式。这样的观点是片面的,当你深入了解到Java的本质之后,你会发现其实并不像你想象的那么简单。

Prerequisite先决条件

What is the essential feature of Java, which has influenced the decision in the toolkit design? Or what is the prerequisite of a java GUI toolkit?

什么才是Java本质的,影响到工具集设计的特征呢?或者说,什么才是Java GUI工具集设计的先决条件呢?

The answer comes from one of Sun's promises about Java, write once, run anywhere. It is one of the java's advantages over other languages. Before Java was created, software portability was a nightmare especially to those who wanted to support multiple platforms. It is especially true in modern days when the Internet is so popular. People from different parts of the world are working on different platforms. And it is very common for a software vendor to support multiple operating systems. Java’s write-once-run-anywhere promise was obvious a relief to developers. It can greatly improve software development productivity.

答案来自于Sun对Java的承诺之一:write once, run anywhere(一次编写,随处运行)。这是Java不同于其他语言的优势所在。在Java被创建之前,软件的跨平台性能是开发者,特别是那些希望对多平台提供支持的开发者的梦魇。在当今的生活中Internet的使用已经相当的普遍了,在世界不同角落的人们在不同的平台上工作着。软件提供商为不同的操作系统提供支持是再平凡不过的事情。Java的write-once-run-anywhere(WORA)承诺显然减轻了开发者的负担,极大地提高了软件开发的生产力。

However to write a portable applications, you should use those standard libraries, which provide platform-independent APIs. These standard libraries include language support, common utilities, networking, I/O and GUI toolkit, etc. So when Sun started to design the GUI toolkit, the first thing it should consider is a well-designed platform-independent API. AWT and Swing were designed carefully in such a way to avoid platform incompatibility. SWT on the contrary was initially designed without this portability in mind. It was first designed for an IDE, Visual Age for Java. And at that time, it seemed that Windows was their first priority. Therefore SWT API is more akin those of Windows. In general SWT is not as portable as Swing. Although Steve Northover, the father of SWT, argued that SWT is platform independent, you can easily find many Windows API inheritances.

然而编写跨平台的应用程序,你必须使用支持平台无关性的标准库。这些标准库包括语言支持,公共用途,网络,I/O和GUI工具集等。所以当Sun开始设计GUI工具集的时候,首要任务就是考虑一个设计良好的平台无关的API。AWT和Swing都被小心地设计以保证平台兼容性。SWT则相反,它在设计之初并不以扩展性为原则,它为一个专有的IDE Visual Age for Java而设计,Windows作为这个IDE的首选运行环境拥有很高的优先级考量。SWT API类似于WIndows,通常它并不如Swing的扩展性好,尽管Steve Northover,SWT之父,辩称SWT是平台无关的,你可以很容易地发现许多Windows API的痕迹。[Windows API的痕迹,并不是一个程序的问题,却是一个跨平台程序的问题,因为多数用linux或mac的人,对Windows上能提供的东西并不感兴趣。]

Differences区别

GUI application is one of the major types of softwares. So Java GUI library should be standardized and integrated into the JRE platform. However different operating systems have different GUI styles and component sets. There are some components which exist on all the platforms and have similar look and feels. These common components such as button, label, textfield, checkbox, etc are also called standard components. Different gui toolkits provide different set of components. Same component from different toolkits may have different look and feel. GUI toolkit usually follows different principle when choosing component types and features to implement. When examining a gui toolkit, we generally have two different levels of concerns. One is component types, the other is component features.

GUI应用程序是软件的一种主要类型,所以Java的GUI库应该是标准化并被集成到JRE平台中的。然而不同的操作系统有不同的GUi风格和组件集。有一些组件在所以平台上有相似的观感。这些共有组件如按钮,标签,文本域,单选框等被称为标准组件。不同的GUI工具集提供了不同的组件集。GUI工具集总是遵循不同的原则来选择组件类型和特征以实现。考察一个工具集,有两个不同的要素:组件类型和组件特征。

Terms

First let me illustrate two mathematical concepts to you: LCD and . LCD means least common denominator. means greatest common denominator. Look at the figure below. There are three different sets standing for different Oses. The intersecting part is LCD of the three, while the union is of the three.

首先让我图解两个数学概念:最大公约数和最小公倍数。如下图,三个集合代表不同的操作系统。相交的部分是最大公约数,合并的部分是最小公倍数。



Component Types and Features组件类型和特征


Now let’s examine the component types and features of the three Java GUI toolkits, AWT, SWT and Swing.

现在让我们来考察Java GUI工具集AWT,SWT和Swing的组件类型和特征

AWT


AWT component set complies LCD principle, which means AWT only has the common set of components which exist on all platforms. So you cannot find advanced components such as table or tree in AWT, because these components are not supported on some other platforms. As to feature set per component, AWT complies LCD too. It can only support those features available from all platforms. For example, AWT buttons is not able to be attached with an icon, because on Motif platform, button is not supposed to have an icon.

AWT组件集遵循最大公约数原则,即AWT只拥有所有平台上都存在的组件的公有集合。所以你在AWT中无法获取如表或树等高级组件,因为它们在某些平台上不支持。AWT的组件特征同样遵循这一原则。它只提高平台上公有的特征。例如AWT按钮不能附着图片,因为在Motif平台上,按钮是不支持图片的。

Since its poor component and feature support, AWT did not attract developers much. And it is deprecated by Sun. It is only there to ensure backward compatibility and support Swing.

由于它低劣的组件集和特征,AWT无法吸引开发者。它是Sun不推荐使用的,只是为了确保向下兼容和支持Swing。

SWT

One of SWT’s initial goal is to provide a more rich component set than AWT. It adopts (greatest common denominator) principle to provide a union set of components that appear on every platform. The idea is that if a component type exists on the platform, then SWT will encapsulate it using java code and JNI call. If a component does not exist on the platform, it then emulates the component by extending and drawing a Composite. A SWT Composite is similar to AWT Canvas. By this way, SWT provides a richer component set than AWT. It is worth to point out that SWT JNI wrapper is different from AWT. Its emulation is also different from Swing.

SWT最初的目标之一是为了提供比AWT更为丰富的组件集。它遵循最小公倍数原则以提供一个各个平台上包含的组件的并集。思路是如果一个组件在某个平台上包含,那么SWT就会包装它并用java代码和JNI来调用它。如果一个组件在某一平台上不存在,它就会用继承并绘制Composite的方式来模拟组件。一个SWT Composite类似于AWT的Canvas。以这种方式,SWT提供了较AWT更为丰富的组件集。值得指出的是SWT的JNI封装不同于AWT,它的模拟也不同于Swing。

As to component feature set, SWT is similar to AWT. They complies to LCD. In early SWT versions, SWT button did not support icon attachment for the same reason as AWT. But later, many of those missing features were made up using emulations. But still, there are features which cannot be implemented purely by emulation. SWT has its components completely controlled native operating system. It is hard to extend. Only features like some graphic decoration can customized by emulating. So strictly speaking, SWT component feature set can not be as rich as Swing due to its difficult to extend.

在组件特征方面,SWT类似于AWT。它遵循最大公约数原则。在早期的SWT版本中,SWT按钮因为和AWT同样的原因不支持附着图片。在之后的版本中,许多缺失的特征采用模拟的方式补全。但仍有许多特征无法采用纯粹的模拟实现。SWT将组件的控制交给本地操作系统。它难以扩展。只有例如图形装饰等特征可以借助模拟绘制来自定义实现。所以严格意义上将,SWT组件的组件集和特征因其难于扩展而不如Swing来得丰富。

Swing

Swing is the most powerful and flexible of the three. With respect to component types, Swing complies to greatest common denominator. Because Swing controls all of the GUI system and it is very extensible and flexible, Swing can almost create any component you could imagine. The only limitation of Swing is its AWT containers. In Swing you still can not implement real transparent or irregular-shaped windows, because Swing depends on those AWT top containers including Applet, Window, Frame and Dialog etc. Except these niches, Swing has implemented almost all the standard components on every platform.

Swing是三者中最强大和灵活的。在组件类型上,它遵循最小公约数原则。由于Swing可以控制自身GUI系统的全部并有很好的可扩展和灵活性,它几乎可以创建所有你想象得到的组件。唯一的限制是它的AWT容器。在Swing中你还不能跨平台地实现真正的透明化和不规则矩形窗口,因为Swing依赖于AWT顶层容器例如Applet, Window, Frame and Dialog等。除此之外,Swing几乎实现了所有平台上的标准组件。[都说Java7中能够实现,也有一些迹象看来接近,期待吧]

As to component feature set, Swing complies to greatest common denominator. It has most of the component features available on every platform. What's more, you can extend the existing Swing components and add more features to them.

在组件特征上,Swing遵循最小公倍数原则。它拥有所有平台上可提供的组件特征。不仅如此,你还可以继承已有的Swing组件并添加新的特性。

(To be continued ...)

 

 

分享到:
评论
1 楼 jvincent 2007-11-27  
喜欢Swing..

相关推荐

    java 图形界面 java awt swt swing gui

    在Java中,有三个主要的GUI工具包:AWT(Abstract Window Toolkit),SWT(Standard Widget Toolkit)和Swing。 1. AWT(Abstract Window Toolkit): AWT是Java最早提供的GUI库,它是Java的基础GUI组件集合。AWT...

    JAVA图形编程(AWT+SWING)

    通过`java图形编程1-AWT.chm`和`java图形编程2-Swing.chm`这两个文档,你可以深入学习AWT和Swing的使用方法,了解组件的创建、布局管理、事件处理、模型-视图-控制器模式,以及如何利用Swing的高级功能来构建复杂的...

    AWT与Swing使用区别

    AWT(Abstract Window Toolkit)和Swing都是Java用于构建图形用户界面(GUI)的库,它们各有特点,但在很多方面存在显著差异。了解这些差异有助于开发者根据项目需求选择合适的技术。 1. **组件的实现方式** - **...

    图形界面开发--AWT,Swing,SWT

    在介绍AWT、Swing、SWT之前,我们先来简单了解一下图形用户界面(GUI)的概念及其在Java开发中的重要性。随着计算机技术的发展,用户与计算机之间的交互方式也在不断进化。从最初的命令行界面(CLI)到如今广泛使用的...

    深入解析Java GUI编程:AWT与Swing的对比

    Java提供了多种图形用户界面(GUI)编程的选项,其中最基础的两种是抽象窗口工具集(Abstract Window Toolkit,简称AWT)和Swing。这两种技术都允许开发者创建和管理GUI组件,但它们在设计哲学、功能和使用方式上...

    Java GUI SWT/Swing/AWT的介绍及比较

    它提供了三个主要的工具包:SWT(Standard Widget Toolkit)、Swing和AWT(Abstract Window Toolkit)。这三个工具包各有特点,适用于不同的开发场景。 AWT是Java最早的GUI工具包,它是Java平台的基础,包含了基本...

    java GUI资料 包括Swing awt

    3. **第十八章_图形界面开发--AWT,Swing,SWT.pdf** - 这个章节可能涵盖了AWT、Swing以及另一个GUI库SWT(Standard Widget Toolkit)的比较和使用,SWT是Eclipse IDE所采用的GUI库,它结合了AWT和Swing的优点。...

    浅析Java中Swing与SWT和AWT的区别及实现_王亚南

    Java是一种广泛使用的编程语言,在图形用户界面(GUI)设计领域提供了多种开发包,其中比较著名的有Swing、SWT和AWT。这三种技术各有特点和适用场景,在开发Java图形界面应用时,开发者需要根据实际需求选择最合适的...

    如何在SWT中使用AWT、SWING组件

    在Java编程环境中,SWT(Standard Widget Toolkit)和AWT(Abstract Window Toolkit)以及SWING是三种常用的图形用户界面(GUI)开发库。SWT主要为Eclipse IDE提供支持,而AWT和SWING则是Java标准库的一部分。这篇...

    100个JavaGUI+swingDemo大全

    1. **Java GUI基础**:Java GUI是基于AWT(Abstract Window Toolkit)框架构建的,但AWT的组件相对有限,外观也受限于操作系统。Swing是在AWT之上建立的,提供了一套更丰富、功能更强的组件,并支持跨平台的统一界面...

    利用AWT和Swing实现Java个人空间应用程序源代码

    在Java编程领域,AWT(Abstract Window Toolkit)和Swing是两个重要的图形用户界面(GUI)工具包,用于创建桌面应用程序。本项目“利用AWT和Swing实现Java个人空间应用程序源代码”提供了构建一个集成了计算器、...

    在eclipse下搭建swing/awt/swt开发环境的方法和插件

    在Java编程领域,Swing、AWT和SWT都是用于构建图形用户界面(GUI)的框架。本篇文章将详细介绍如何在Eclipse集成开发环境中(IDE)搭建这些框架的开发环境,以及可以使用的相关插件。 首先,让我们从AWT(Abstract ...

    浅析Java GUI编程工具集.pdf

    本文主要介绍了Java GUI编程的三个主要工具集:AWT、Swing和SWT,并对其优缺点进行了比较。下面将对标题、描述、标签和部分内容进行详细的解释和分析。 标题: 浅析Java GUI编程工具集 标题表明了本文的主要内容,...

    使用Java AWT和Swing实现的简单计算器

    Java AWT(Abstract Window Toolkit)和Swing是Java平台上的两个图形用户界面(GUI)工具包,用于构建桌面应用程序。本项目"使用Java AWT和Swing实现的简单计算器"展示了如何利用这两个库创建一个基本的计算器应用。...

    java实验报告之图形用户界面程序设计与 AWT/Swing组件

    在本实验报告中,主题是“Java实验报告之图形用户界面程序设计与AWT/Swing组件”,主要目标是让学生熟悉Java GUI设计原理、程序结构,掌握AWT和Swing组件的功能,以及学会应用这些组件来设计应用程序,特别是实现一...

    Java GUI实例下载

    4. **Swing组件**:Swing是Java GUI的主要工具包,它构建在AWT(Abstract Window Toolkit)之上,提供了更为丰富和现代的组件,如JFrame、JPanel、JScrollPane等。Swing组件是轻量级的,意味着它们在性能上优于AWT,...

    JAVA GUI编程实例集.rar

    1. **Java AWT和Swing库**:Java提供两种主要的GUI工具包,Abstract Window Toolkit (AWT) 和 Swing。AWT是Java早期的GUI库,基于操作系统原生组件,而Swing是建立在AWT之上的轻量级框架,提供了更丰富的组件和更好...

    SWT_AWT_Swing事件处理机制

    在Java编程语言中,SWT(Standard Widget Toolkit)、AWT(Abstract Window Toolkit)和Swing是用于构建图形用户界面(GUI)的三个重要库。这三个库各有特点,但都提供了事件处理机制,允许用户与应用程序进行交互。...

    Java图形界面开发-awt、swing、swt

    Java提供了多种库来支持图形界面开发,包括AWT(Abstract Window Toolkit)、Swing和SWT(Standard Widget Toolkit)。这些库各自有不同的特点和应用场景。 - **AWT**:这是Java最早提供的图形界面工具包,主要通过...

Global site tag (gtag.js) - Google Analytics