有人能给我解释一下Swing和AWT的区别吗?
在某些情况下,AWT比swing更有用/更被建议使用,或者反之亦然?
有人能给我解释一下Swing和AWT的区别吗?
在某些情况下,AWT比swing更有用/更被建议使用,或者反之亦然?
当前回答
AWT 1。AWT占用更多的内存空间 2。AWT依赖于平台 3.AWT需要javax。awt包
波动 1。Swing占用较少的内存空间 2。Swing组件是平台独立的 3.Swing需要javax。摇摆不定的包
其他回答
The base difference that which already everyone mentioned is that one is heavy weight and 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.
Java 8
摇摆不定的
它是Java基础类的一部分 Swing是建立在AWT之上的 Swing组件是轻量级的 Swing支持可插入的外观和感觉 平台无关的 采用MVC:模型-视图-控制器架构 包:javax.swing 与Swing的其他组件不同, 它们是轻量级的,顶级容器是重量级的。
AWT -抽象窗口工具包
平台的依赖 AWT组件是重量级的 包java.awt
AWT 1。AWT占用更多的内存空间 2。AWT依赖于平台 3.AWT需要javax。awt包
波动 1。Swing占用较少的内存空间 2。Swing组件是平台独立的 3.Swing需要javax。摇摆不定的包
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.
Swing组件被称为“轻量级”,因为它们不需要 来实现它们的功能。JDialog和JFrame是 重量级的,因为他们有对手。比如JButton, JTextArea等是轻量级的,因为它们没有操作系统对等体。
对等体是操作系统提供的小部件,例如 按钮对象或输入字段对象。
AWT是一个Java接口,可以连接到操作系统中的本机系统GUI代码。它不会在每个系统上都一样,尽管它尝试了。
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工具包的两种截然不同的方法,并且会产生很多后果。对你的问题的完整回答将尝试探索所有这些问题。:)以下是一些例子:
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 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访问本机widget的AWT思想的回归。