Swing: Difference between revisions
CSV import Tags: mobile edit mobile web edit |
CSV import |
||
| Line 1: | Line 1: | ||
Swing | |||
{{ | |||
{{ | Swing is a [[Java (programming language)|Java]]-based graphical user interface (GUI) toolkit that is part of the Java Foundation Classes (JFC). It provides a set of "lightweight" (all-Java language) components that work the same on all platforms, allowing developers to create cross-platform applications with a consistent look and feel. | ||
== Overview == | |||
Swing was developed by [[Sun Microsystems]] and is now maintained by [[Oracle Corporation]] as part of the Java Standard Edition. It was introduced in 1997 as part of the Java Development Kit (JDK) 1.2. Swing is built on top of the Abstract Window Toolkit ([[AWT]]), which is the original Java GUI toolkit. | |||
Swing provides a richer set of GUI components than AWT, including advanced components like trees, tables, and text areas. It also supports pluggable look and feel, allowing applications to mimic the appearance of native applications on different platforms or to have a unique look. | |||
== Features == | |||
* '''[[Lightweight Components]]''': Unlike AWT components, which are "heavyweight" and rely on the native system's GUI components, Swing components are "lightweight" and written entirely in Java. | |||
* '''[[Pluggable Look and Feel]]''': Swing allows developers to change the appearance of their applications by switching between different "look and feel" themes, such as the default "Metal" look, "Nimbus", or the native look of the operating system. | |||
* '''[[MVC Architecture]]''': Swing components follow the Model-View-Controller (MVC) design pattern, which separates the data (model), the user interface (view), and the interaction logic (controller). | |||
* '''[[Customizable Components]]''': Developers can create custom components by extending existing Swing components or by creating new ones from scratch. | |||
* '''[[Event Handling]]''': Swing uses the event delegation model for handling user interactions, which is more efficient and flexible than the event handling model used in AWT. | |||
== Components == | |||
Swing provides a wide range of components, including: | |||
* '''[[JButton]]''': A push button that can trigger an action when clicked. | |||
* '''[[JLabel]]''': A display area for a short text string or an image. | |||
* '''[[JTextField]]''': A single-line text input field. | |||
* '''[[JTextArea]]''': A multi-line area to display/edit text. | |||
* '''[[JTable]]''': A component that displays data in a two-dimensional table format. | |||
* '''[[JTree]]''': A component that displays a hierarchical tree of data. | |||
* '''[[JPanel]]''': A generic container for holding other components. | |||
== Usage == | |||
To use Swing in a Java application, developers typically import the `javax.swing` package and create a class that extends `JFrame` or another top-level container. They then add components to the frame and set up event listeners to handle user interactions. | |||
Example: | |||
```java | |||
import javax.swing.*; | |||
import java.awt.event.*; | |||
public class HelloWorldSwing { | |||
public static void main(String[] args) { | |||
// Create a new JFrame | |||
JFrame frame = new JFrame("Hello World Swing"); | |||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |||
// Create a label | |||
JLabel label = new JLabel("Hello, World!"); | |||
frame.getContentPane().add(label); | |||
// Display the window | |||
frame.pack(); | |||
frame.setVisible(true); | |||
} | |||
} | |||
``` | |||
== Also see == | |||
* [[JavaFX]] | |||
* [[Abstract Window Toolkit]] | |||
* [[Java Development Kit]] | |||
* [[Model-View-Controller]] | |||
{{Java}} | |||
{{Software}} | |||
[[Category:Java platform]] | |||
[[Category:Java APIs]] | |||
[[Category:Graphical user interfaces]] | |||
Latest revision as of 22:43, 15 December 2024
Swing
Swing is a Java-based graphical user interface (GUI) toolkit that is part of the Java Foundation Classes (JFC). It provides a set of "lightweight" (all-Java language) components that work the same on all platforms, allowing developers to create cross-platform applications with a consistent look and feel.
Overview[edit]
Swing was developed by Sun Microsystems and is now maintained by Oracle Corporation as part of the Java Standard Edition. It was introduced in 1997 as part of the Java Development Kit (JDK) 1.2. Swing is built on top of the Abstract Window Toolkit (AWT), which is the original Java GUI toolkit.
Swing provides a richer set of GUI components than AWT, including advanced components like trees, tables, and text areas. It also supports pluggable look and feel, allowing applications to mimic the appearance of native applications on different platforms or to have a unique look.
Features[edit]
- Lightweight Components: Unlike AWT components, which are "heavyweight" and rely on the native system's GUI components, Swing components are "lightweight" and written entirely in Java.
- Pluggable Look and Feel: Swing allows developers to change the appearance of their applications by switching between different "look and feel" themes, such as the default "Metal" look, "Nimbus", or the native look of the operating system.
- MVC Architecture: Swing components follow the Model-View-Controller (MVC) design pattern, which separates the data (model), the user interface (view), and the interaction logic (controller).
- Customizable Components: Developers can create custom components by extending existing Swing components or by creating new ones from scratch.
- Event Handling: Swing uses the event delegation model for handling user interactions, which is more efficient and flexible than the event handling model used in AWT.
Components[edit]
Swing provides a wide range of components, including:
- JButton: A push button that can trigger an action when clicked.
- JLabel: A display area for a short text string or an image.
- JTextField: A single-line text input field.
- JTextArea: A multi-line area to display/edit text.
- JTable: A component that displays data in a two-dimensional table format.
- JTree: A component that displays a hierarchical tree of data.
- JPanel: A generic container for holding other components.
Usage[edit]
To use Swing in a Java application, developers typically import the `javax.swing` package and create a class that extends `JFrame` or another top-level container. They then add components to the frame and set up event listeners to handle user interactions.
Example:
```java import javax.swing.*; import java.awt.event.*;
public class HelloWorldSwing {
public static void main(String[] args) {
// Create a new JFrame
JFrame frame = new JFrame("Hello World Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label
JLabel label = new JLabel("Hello, World!");
frame.getContentPane().add(label);
// Display the window
frame.pack();
frame.setVisible(true);
}
} ```
Also see[edit]
{{{1}}}
Template:Software