Swing is a development toolkit (GUI toolkit) for developing Java application user interfaces. It is part of the JAVA basic class; it includes graphical user interface (GUI) devices , such as: text boxes, buttons, split panes and tables.
Swing provides many better screen display elements than AWT. They are written in pure Java, so they can run cross-platform like Java itself, unlike AWT. They are part of JFC. They support replaceable panels and themes (default specific themes for various operating systems), but do not actually use the devices provided by the native platform, but only superficially imitate them. This means you can use any panel supported by JAVA on any platform. The disadvantage of lightweight components is that they execute slowly, but the advantage is that they can adopt uniform behavior on all platforms.
Swing class library structure
Swing components all adopt the MVC (Model-View-Controller, model-view-controller) design to realize the display of GUI components. The separation of logic and data logic allows programmers to customize Render to change the display appearance of GUI components to provide more flexibility.
Swing is built around the JComponent component, which extends from the AWT container class.
Swing class library organization chart:
#It can be seen that Swing components except the AbstmctButton class all start with J. The Swing container component directly inherits the container component class in the AWT class library, and most other components inherit the JComponet component. Components can be divided into container components and non-container components. Container components include JFmme and JDialog. Among them, JComponent defines lightweight components of non-container classes (JBntton, JPanel, JMenu, etc.).
Swing package
The Swing class library is composed of many packages, and the GUI design is completed through the classes in these packages. Among them, the javax.swing package is the largest package provided by Swing, which contains nearly 100 classes and 25 interfaces. Almost all Swing components are in this package. Table 1 lists commonly used Swing packages.
Swing common packages
Package name |
Description |
##javax.swing | Provide a set of "lightweight" components that try to work the same way on all platforms |
javax.swing.border | Provides classes and interfaces for drawing special borders around Swing components |
javax.swing.event | Provides events triggered by Swing components |
javax.swing.filechooser | Provides classes and interfaces used by the JFileChooser component |
javax.swing.table | Provides classes and interfaces for processing javax. Classes and interfaces of swing.JTable |
javax.swing.text | Provides class HTMLEditorKit and support classes for creating HTML text editors |
javax.swing.tree | Provides classes and interfaces for processing javax.swingJTree |
The javax.swing.event package defines events and event listener classes. The javax.swing.event package is similar to the AWT event package. Both Java.awt.event and javax.swing.event contain event classes and listener interfaces that respond to events triggered by AWT components and Swing components respectively.
For example, when you need notification of node expansion (or collapse) in a tree component, you need to implement Swing's TreeExpansionListener interface and pass a TreeExpansionEvent instance to the method defined in the TreeExpansionListener interface, and TreeExpansionListener and TreeExpansionEvent They are all defined in the swing.event package.
Although Swing's table component (JTable) is in the javax.swing package, its support classes are in the javax.swing.table package. Table models, graphics drawing classes and editors are also in the javax.swing.table package.
Like the JTable class, the tree JTree in Swing (the structural component used to organize data hierarchically) is also in the javax.swing package, and its supporting classes are in the javax.swing.tree package. The javax.swing.tree package provides support classes such as tree models, tree nodes, tree unit editing classes, and tree drawing classes.
Swing Container
The first step in creating a graphical user interface program is to create a container class to accommodate other components. A common window is a container. The container itself is also a component, and its role is to organize, manage and display other components.
Containers in Swing can be divided into two categories: top-level containers and intermediate containers.
The top-level container is the basis for graphics programming. All graphical things must be included in the top-level container. The top-level container is the main window that any graphical interface program involves. It is a container component that displays and hosts components. There are three top-level containers that can be used in Swing, namely JFrame, JDialog and JApplet.
● JFrame: Class used for frame windows with borders, titles, icons for closing and minimizing the window. Applications with a GUI use at least one frame window.
● JDialog: Class used for dialog boxes.
● JApplet: Java Applet class for using Swing components.
The intermediate container is a type of container component and can also host other components. However, the intermediate container cannot be displayed independently and must be attached to other top-level containers. Common intermediate containers include JPanel, JScrollPane, JTabbedPane and JToolBar.
● JPanel: Represents an ordinary panel and is the most flexible and commonly used intermediate container.
● JScrollPane: Similar to JPanel, but it provides scroll bars around large or expandable components.
● JTabbedPane: Represents a tabbed panel, which can contain multiple components, but only one component is displayed at a time. Users can easily switch between components.
● JToolBar: Represents a toolbar, a set of components (usually buttons) arranged in rows or columns.
Container classes in Java programs all inherit from the Container class. The intermediate container and the top-level container inherit the inheritance relationship of the Container class in the AWT package and Swing package, as shown in the figure (container class organization chart):
Recommended learning: Java video tutorial
The above is the detailed content of What is java swing?. For more information, please follow other related articles on the PHP Chinese website!