Java Swing 和 AWT 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/408820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Swing and AWT?
提问by Samiksha
Can someone please explain me what's the difference between Swing and AWT?
有人可以解释一下 Swing 和 AWT 之间的区别吗?
Are there any cases where AWT is more useful/advised to use than swing or vice-versa?
是否有任何情况下 AWT 比 Swing 更有用/建议使用,反之亦然?
采纳答案by skiphoppy
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.
AWT 是操作系统中存在的本机系统 GUI 代码的 Java 接口。尽管它会尝试,但它不会在每个系统上都相同。
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).
Swing 或多或少是纯 Java GUI。它使用 AWT 创建一个操作系统窗口,然后将按钮、标签、文本、复选框等的图片绘制到该窗口中,并响应您的所有鼠标点击、键输入等,自行决定要做什么而不是让操作系统处理它。因此,Swing 是 100% 可移植的,并且跨平台是相同的(尽管它是可换肤的并且具有“可插入的外观和感觉”,可以使它看起来或多或少像本机窗口和小部件的外观)。
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:
这些是非常不同的 GUI 工具包方法,会产生很多后果。您的问题的完整答案将尝试探索所有这些。:) 这里有几个:
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 工具包来实现其功能,它也不提供对这些工具包可以执行的所有操作的访问。一个平台上可能存在的高级或更新的 AWT 小部件可能在另一个平台上不受支持。可能不支持在每个平台上都不同的小部件的功能,或者更糟的是,它们在每个平台上的工作方式可能不同。人们过去常常投入大量精力来让他们的 AWT 应用程序跨平台一致地工作——例如,他们可能会尝试从 Java 调用本地代码。
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.
因为 AWT 使用本机 GUI 小部件,所以您的操作系统知道它们并处理将它们放在彼此的前面等等,而从您的操作系统的角度来看,Swing 小部件是窗口内无意义的像素。Swing 本身处理小部件的布局和堆叠。混合使用 AWT 和 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.
因为除了原生 GUI 窗口提供的非常原始的图形例程之外,Swing 尝试在 Java 中做所有可能的事情,所以与 AWT 相比,它过去会导致相当大的性能损失。不幸的是,这使得 Swing 的流行速度很慢。然而,由于更优化的 JVM、更快的机器以及(我认为)Swing 内部结构的优化,这在过去几年中急剧下降。今天,Swing 应用程序可以运行得足够快,可以提供服务,甚至可以快速运行,并且几乎与使用本机小部件的应用程序无法区分。有些人会说达到这一点花了太长时间,但大多数人会说这是非常值得的。
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.
最后,您可能还想查看 SWT(用于 Eclipse 的 GUI 工具包,是 AWT 和 Swing 的替代品),这在某种程度上是对通过 Java 访问本机小部件的 AWT 想法的回归。
回答by cletus
Swing vs AWT. Basically AWT came first and is a set of heavyweight UI components (meaning they are wrappers for operating system objects) whereas Swing built on top of AWT with a richer set of lightweight components.
摇摆与 AWT。基本上,AWT 首先出现并且是一组重量级 UI 组件(意味着它们是操作系统对象的包装器),而 Swing 构建在 AWT 之上,具有一组更丰富的轻量级组件。
Any serious Java UI work is done in Swing not AWT, which was primarily used for applets.
任何严肃的 Java UI 工作都是在 Swing 中完成的,而不是 AWT,后者主要用于小程序。
回答by Access Denied
The base difference that which already everyone mentioned is that one is heavy weightand other is light weight. Let me explain, basically what the term heavy weight means is that when you're using the awt components the native code used for getting the view component is generated by the Operating System, thats why it the look and feel changes from OS to OS.Where as in swing components its the responsibility of JVM to generate the view for the components.Another statement which i saw is that swing is MVC based and awt is not.
每个人已经提到的基本区别是一个是重量级的,另一个是重量级的。让我解释一下,重量级这个术语的基本含义是,当您使用 awt 组件时,用于获取视图组件的本机代码是由操作系统生成的,这就是为什么它的外观会随着操作系统而变化。在 Swing 组件中,JVM负责为组件生成视图。我看到的另一个声明是,swing 是基于 MVC 的,而 awt 不是。
回答by Nate
As far as when AWT may be more useful than Swing -
至于什么时候 AWT 可能比 Swing 更有用 -
- you may be targeting an older JVM or platform that doesn't support Swing. This used to really come into play if you were building Applets - you wanted to target the lowest common denominator so people wouldn't have to install a newer Java plugin. I'm not sure what the current most widely installed version of the Java plugin is - this may be different today.
- some people prefer the native look of AWT over Swing's 'not quite there' platform skins. (There are better 3rd party native looking skins than Swing's implementations BTW) Lots of people preferred using AWT's FileDialog over Swing's FileChooser because it gave the platform file dialog most people were used to rather than the 'weird' custom Swing one.
- 您的目标可能是不支持 Swing 的较旧的 JVM 或平台。如果您正在构建 Applet,这曾经真正发挥作用 - 您希望以最小公分母为目标,这样人们就不必安装更新的 Java 插件。我不确定当前安装最广泛的 Java 插件版本是什么 - 今天可能有所不同。
- 有些人更喜欢 AWT 的原生外观,而不是 Swing 的“不太有”平台外观。(有比 Swing 的实现更好的 3rd 方本机外观皮肤)很多人更喜欢使用 AWT 的 FileDialog 而不是 Swing 的 FileChooser,因为它提供了大多数人习惯的平台文件对话框,而不是“奇怪的”自定义 Swing 对话框。
回答by arsh
- swing component provide much flexible user interface because it follow model view controller(mvc).
- awt is not mvc based.
- swing works faster.
- awt does not work faster.
- swing components are light weight.
- awt components are heavy weight.
- swing occupies less memory space.
- awt occupies more memory space.
- swing component is platform independent.
- awt is platform dependent.
- swing require javax.swing package.
- awt require javax.awt package.
- Swing 组件提供了非常灵活的用户界面,因为它遵循模型视图控制器(mvc)。
- awt 不是基于 mvc 的。
- 摆动工作得更快。
- awt 不能更快地工作。
- 摆动部件重量轻。
- awt 组件很重。
- Swing 占用的内存空间较少。
- awt 占用更多的内存空间。
- Swing 组件是独立于平台的。
- awt 依赖于平台。
- Swing 需要 javax.swing 包。
- awt 需要 javax.awt 包。
回答by deepika
Swing:
摇摆:
- Swing is part of the java foundation classes.
- Swing components are platform-independent.
- Swing components are lightweight components because swing sits on the top of awt.
- Swing 是 java 基础类的一部分。
- Swing 组件与平台无关。
- Swing 组件是轻量级组件,因为 Swing 位于 awt 的顶部。
AWT:
AWT:
- AWT is called the abstract window tool.
- AWT components are platform-dependent.
- AWT components are heavyweight components.
- AWT 被称为抽象窗口工具。
- AWT 组件是平台相关的。
- AWT 组件是重量级组件。
回答by B.DastagiriReddy
AWT1 . AWT occupies more memory space 2 . AWT is platform dependent 3 . AWT require javax.awt package
AWT1。AWT 占用更多的内存空间 2 。AWT 是平台相关的 3 。AWT 需要 javax.awt 包
swings1 . Swing occupies less memory space 2 . Swing component is platform independent 3 . Swing requires javax.swing package
波动1。Swing 占用的内存空间较少 2.Swing 组件是独立于平台的 3。Swing 需要 javax.swing 包
回答by Surendra Bharadwaj
Several consequences result from this difference between AWT and Swing.
AWT 和 Swing 之间的这种差异导致了几个后果。
AWT is a thin layer of code on top of the OS, whereas Swing is much larger. Swing also has very much richer functionality. Using AWT, you have to implement a lot of things yourself, while Swing has them built in. For GUI-intensive work, AWT feels very primitive to work with compared to Swing. Because Swing implements GUI functionality itself rather than relying on the host OS, it can offer a richer environment on all platforms Java runs on. AWT is more limited in supplying the same functionality on all platforms because not all platforms implement the same-looking controls in the same ways.
AWT 是操作系统之上的一层薄代码,而 Swing 则要大得多。Swing 还具有非常丰富的功能。使用 AWT,您必须自己实现很多东西,而 Swing 内置了它们。对于 GUI 密集型工作,与 Swing 相比,使用 AWT 感觉非常原始。因为 Swing 自己实现 GUI 功能而不是依赖主机操作系统,所以它可以在 Java 运行的所有平台上提供更丰富的环境。AWT 在所有平台上提供相同的功能方面受到更多限制,因为并非所有平台都以相同的方式实现外观相同的控件。
Swing components are called "lightweight" because they do not require a
native OS object to implement their functionality. JDialog
and JFrame
are
heavyweight, because they do have a peer. So components like JButton
,
JTextArea
, etc., are lightweight because they do not have an OS peer.
Swing 组件被称为“轻量级”,因为它们不需要本机 OS 对象来实现其功能。JDialog
并且JFrame
是重量级的,因为他们确实有同行。因此,像JButton
、
JTextArea
等组件是轻量级的,因为它们没有操作系统对等体。
A peeris a widget provided by the operating system, such as a button object or an entry field object.
阿对等是由操作系统,提供诸如按钮对象或记录域对象的小部件。