Swing 和 AWT 有什么区别? [英] What is the difference between Swing and AWT?

查看:21
本文介绍了Swing 和 AWT 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 Swing 和 AWT 的区别是什么?

Can someone please explain me what's the difference between Swing and AWT?

在任何情况下,AWT 是否比 Swing 更有用/更建议使用,反之亦然?

Are there any cases where AWT is more useful/advised to use than swing or vice-versa?

推荐答案

AWT 是您的操作系统中本机系统 GUI 代码的 Java 接口.尽管它会尝试,但它不会在每个系统上都一样工作.

AWT is a Java interface to native system GUI code present in your OS. It will not work the same on every system, although it tries.

Swing 或多或少是纯 Java GUI.它使用 AWT 创建一个操作系统窗口,然后将按钮、标签、文本、复选框等的图片绘制到该窗口中,并响应您的所有鼠标点击、键输入等,自行决定要做什么而不是让操作系统处理它.因此,Swing 是 100% 可移植的,并且跨平台是相同的(尽管它是可换肤的并且具有可插入的外观和感觉",可以使它看起来或多或少像本机窗口和小部件的外观).

Swing is a more-or-less pure-Java GUI. It uses AWT to create an operating system window and then paints pictures of buttons, labels, text, checkboxes, etc., into that window and responds to all of your mouse-clicks, key entries, etc., deciding for itself what to do instead of letting the operating system handle it. Thus Swing is 100% portable and is the same across platforms (although it is skinnable and has a "pluggable look and feel" that can make it look more or less like how the native windows and widgets would look).

这些是非常不同的 GUI 工具包方法,会产生很多后果.您的问题的完整答案将尝试探索所有这些.:) 这里有几个:

These are vastly different approaches to GUI toolkits and have a lot of consequences. A full answer to your question would try to explore all of those. :) Here are a couple:

AWT 是一个跨平台接口,因此即使它使用底层操作系统或本机 GUI 工具包来实现其功能,它也不提供对这些工具包可以执行的所有操作的访问.一个平台上可能存在的高级或更新的 AWT 小部件可能在另一个平台上不受支持.可能不支持在每个平台上都不同的小部件的功能,或者更糟的是,它们在每个平台上的工作方式可能不同.人们过去常常投入大量精力来让他们的 AWT 应用程序跨平台一致地工作 - 例如,他们可能会尝试从 Java 调用本地代码.

AWT is a cross-platform interface, so even though it uses the underlying OS or native GUI toolkit for its functionality, it doesn't provide access to everything that those toolkits can do. Advanced or newer AWT widgets that might exist on one platform might not be supported on another. Features of widgets that aren't the same on every platform might not be supported, or worse, they might work differently on each platform. People used to invest lots of effort to get their AWT applications to work consistently across platforms - for instance, they may try to make calls into native code from Java.

因为 AWT 使用本机 GUI 小部件,所以您的操作系统知道它们并处理将它们放在彼此的前面等,而从您的操作系统的角度来看,Swing 小部件是窗口内无意义的像素.Swing 本身处理小部件的布局和堆叠.混合使用 AWT 和 Swing 是非常不受支持的,可能会导致荒谬的结果,例如本机按钮会掩盖它们所在对话框中的其他所有内容,因为其他所有内容都是使用 Swing 创建的.

Because AWT uses native GUI widgets, your OS knows about them and handles putting them in front of each other, etc., whereas Swing widgets are meaningless pixels within a window from your OS's point of view. Swing itself handles your widgets' layout and stacking. Mixing AWT and Swing is highly unsupported and can lead to ridiculous results, such as native buttons that obscure everything else in the dialog box in which they reside because everything else was created with Swing.

因为除了原生 GUI 窗口提供的非常原始的图形例程之外,Swing 尝试在 Java 中做所有可能的事情,所以与 AWT 相比,它过去会导致相当大的性能损失.不幸的是,这使得 Swing 的流行速度很慢.然而,由于更优化的 JVM、更快的机器以及(我认为)Swing 内部结构的优化,这在过去几年中急剧缩小.今天,Swing 应用程序可以运行得足够快,可以提供服务,甚至可以快速运行,并且几乎与使用本机小部件的应用程序无法区分.有些人会说达到这一点花了太长时间,但大多数人会说这是值得的.

Because Swing tries to do everything possible in Java other than the very raw graphics routines provided by a native GUI window, it used to incur quite a performance penalty compared to AWT. This made Swing unfortunately slow to catch on. However, this has shrunk dramatically over the last several years due to more optimized JVMs, faster machines, and (I presume) optimization of the Swing internals. Today a Swing application can run fast enough to be serviceable or even zippy, and almost indistinguishable from an application using native widgets. Some will say it took far too long to get to this point, but most will say that it is well worth it.

最后,您可能还想查看 SWT(用于 Eclipse 的 GUI 工具包,是 AWT 和 Swing 的替代品),这在某种程度上是对通过 Java 访问本机小部件的 AWT 想法的回归.

Finally, you might also want to check out SWT (the GUI toolkit used for Eclipse, and an alternative to both AWT and Swing), which is somewhat of a return to the AWT idea of accessing native Widgets through Java.

这篇关于Swing 和 AWT 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆