Swing 和 AWT 中,为什么一个被认为是轻量级的,另一个被认为是重量级的?
为什么说JAVA中Swings是重量级的,AWT是轻量级的呢?
Why is it said that Swings is heavy-weight and AWT is light-weight in JAVA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AWT 被称为“重量级”,因为基本上每个 AWT 组件都是本机平台组件。 AWT 是在平台的本机 GUI 工具包之上实现的。
这也解释了为什么 AWT 与 Swing 相比相当有限。 就所实现的内容而言,它使用最小公分母。
另一方面,除了顶级组件(Windows...)之外,Swing 几乎所有内容都是用 Java 实现的。 可以有本机组件,这些组件仍然被称为“重量级”。
查看 IBM 的此页面进行深入比较AWT、Swing 和 SWT。
编辑:我认为这是你的问题,尽管你的措辞中重/轻似乎颠倒了。 重/轻量级几乎是 Java GUI 工具包中的标准名称,所以我按照我的理解进行。 (感谢 BobbyShaftoe 指出这一点)。
AWT is said to be "Heavyweight" because basically each AWT component is a native platform component. AWT is implemented on top of the platform's native GUI toolkit.
This also explains why AWT was pretty limited compared to Swing. It uses the least common denominator as far as what is implemented.
Swing, on the other hand, is implemented in Java for pretty much everything except for top level components (windows...). There can be native components and those are still termed "heavy weight".
Take a look at this page from IBM for an in-depth comparison of AWT, Swing and SWT.
EDIT: I assume that was your question, even though heavy/light seem to be reversed in your phrasing. Heavy/light weight is pretty much a standard denomination in the Java GUI toolkits so I went with my understanding. (thanks to BobbyShaftoe for pointing that out).
在 Java 世界中,AWT 组件被认为是“重量级”,因为它们使用底层本机组件。 当您实例化 java.awt.Button 的实例时,您实际上是在要求底层操作系统为您绘制该对象。
另一方面,Swing 是“轻量级”的,因为它主要依赖于 Java2D API 来完成所有绘制,而绘制又委托给底层操作系统或硬件。 这也解释了为什么 Swing 需要所有那些进行实际绘制的 UI 组件来模仿特定的外观和感觉(Windows、GTK、Motif 等)。
本文更详细地解释了轻量级组件和重量级组件之间的区别.
希望这会有所帮助。
In the Java world, AWT components are considered "heavy-weight" because they use underlying native components. When you instantiate an instance of java.awt.Button, you are actually asking the underlying OS to paint this object for you.
Swing, on the other hand, is "light-weight" because it mostly depends on the Java2D API to do all painting, which in turn delegates to the underlying OS or hardware. This also explains why Swing needs all those UI components that do the actual painting to mimic a specific look and feel (Windows, GTK, Motif, etc.).
This article explains more in detail the difference between light-weight and heavy-weight components.
Hope this helps.
因为每个“重量级”组件都有一个本机对等体,而“轻量级”组件则没有。 由于AWT是重量级的,所以每个AWT Button在windows平台上都有对应的MFC按钮,或者在Unix平台上有Motif按钮。 由于 Swing 是轻量级的,所以按钮的图形只是绘制在框架的顶部...没有底层的 MFC 或 Motif 按钮。 您可以在文章 混合重载和轻载中找到有关此主题的更多信息Sun 网站上的组件。
Because each "heavyweight" component has a native peer, and "lightweight" components do not. Since AWT is heavyweight, each AWT Button has a corresponding MFC button on the windows platform, or Motif button on a Unix platform. Since Swing is lightweight, the graphics of the button is just drawn on top of the frame... there is no underlying MFC or Motif button. You can find more information on this topic in the article Mixing heavy and light components on Sun's website.
我不完全理解这个问题,我确信这会导致一场争论,但也许是因为 AWT 提供了一个“更简单”的 API,尽管它有点原始。 然而,如果您喜欢 API,那么 Swing 可以说是非常头重脚轻或重量级的。 它比 AWT 拥有更丰富的支持和更多的类。
I don't totally understand the question and I'm sure this is going to result in a flamewar but perhaps it is because AWT provides a "simpler" API, though it is a bit more primitive. Swing however, is arguably a very top-heavy, or heavy weight, if you like API. It has far richer support and many more classes than AWT.