Home Java JavaBase What is java swing?

What is java swing?

Dec 26, 2019 pm 06:04 PM
java swing

What is java swing?

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:

What is java swing?

#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.

##javax.swingProvide a set of "lightweight" components that try to work the same way on all platformsjavax.swing.border Provides classes and interfaces for drawing special borders around Swing componentsjavax.swing.eventProvides events triggered by Swing componentsjavax.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.JTablejavax.swing.textProvides class HTMLEditorKit and support classes for creating HTML text editorsjavax.swing.treeProvides 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):

What is java swing?

Recommended learning: Java video tutorial

Swing common packages
Package name Description

The above is the detailed content of What is java swing?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles