Swing (Java)
Swing 是一个为Java设计的GUI工具包。 Swing 是 JAVA基础类 的一部分。 Swing 包括了图形用户界面 (GUI) 器件 如:文本框,按钮,分隔窗格和表。
SWING 提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,而不像AWT。 它们是JFC的一部分。 它们支持可更换的观感和主题(各种操作系统默认的特有主题) — 不是真的使用原生平台提供的设备而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用任意JAVA支持的观感。 轻便元件的缺点是执行速度较慢。 优点就是可以在所有平台上采用统一的行为。
程序
Swing程序
HelloWorldSwing 的代码:
import javax.swing.*; public class HelloWorldSwing { /** * 创建并显示GUI。 出于线程安全的考虑, * 这个方法在事件调用线程中调用。 */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
历史
互联网基础类(IFC) 是网景公司最初为Java开发的图形库,第一次发布于1996年12月16日。
在1997年4月2日,太阳微系统公司 和 网景公司公开了他们将IFC和其他技术合并形成 Java基础类的意图。 作为IFC最初提供组件的附加, Swing 引进了一个机理:不改变大量的程序代码而允许程序中每个组件观感的变化。 支持可更换观感的引入允许Swing组件模拟原生组件的外观同时保持平台独立的优点。
同AWT的关系
Java的早期版本以来, 抽象窗口工具包 为用户界面组件提供了平台独立的API。在AWT中,每个组件都由一个原生的等同组件生成和控制,这个原生组件是由当前的图形窗口系统决定的。
与此相对,Swing 组件经常被描述为轻量级的,因为它们不需要操作系统本身所带窗口工具包的原生资源来生成。
Swing API 的大部分是AWT的补充扩展而不是直接的代替。Swing用来绘制轻量级组件的核心渲染功能是由Java2D提供的,这是AWT的一部分。然而,轻量级和重量级组件在同一个应用中使用会导致Z-order不兼容。
同 SWT 的关系
The Standard Widget Toolkit (SWT) is a competing toolkit originally developed by IBM and now maintained by the Eclipse Foundation. SWT's implementation has more in common with the heavyweight components of AWT. This confers benefits such as more accurate fidelity with the underlying native windowing toolkit, at the cost of an increased exposure to the native resources in the programming model.
The advent of SWT has given rise to a great deal of division among Java desktop developers with many strongly favouring either SWT or Swing. A renewed focus on Swing look and feel fidelity with the native windowing toolkit in the approaching 6.0 release of Java is probably a direct result of this.
参考
- Matthew Robinson: Swing, Manning, ISBN 1-930110-88-X
- David M. Geary: Graphic Java 2, Volume 2: Swing, Prentice Hall, ISBN 0-13-079667-0
- James Elliott, Robert Eckstein, Marc Loy, David Wood, Brian Cole: Java Swing, O'Reilly, ISBN 0-596-00408-7
- Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour: The JFC Swing Tutorial: A Guide to Constructing GUIs, Addison-Wesley Professional, ISBN 0-201-91467-0
外部链接
- The Swing Tutorial
- The Swing Connection
- JavaDesktop
- Java Look And Feel
- ClientJava.com
- Presentation "Professional Swing: Creating Polished Apps, Part 1/2" by Ben Galbraith
- Presentation "Professional Swing: Creating Polished Apps, Part 2/2" by Ben Galbraith
- Article "What does "Swing is Slow" mean?" by Sermet Yucel
- Article "Java Desktop Development" by Andrei Cioroianu
- Article "BlackMamba: A Swing Case Study" by Ashwin Jayaprakash
- Articles on AWT/Swing
- Citations from CiteSeer
- Swing wiki containing best practices, tips, tricks and howto tutorials