Home > Java > javaTutorial > body text

What is swing in java

青灯夜游
Release: 2019-05-05 11:49:02
Original
12176 people have browsed it

Swing is a development toolkit for developing Java application user interfaces, that is, the UI of the Java platform; it acts as the software that handles all interactions between the user and the computer, and is actually the middleman between the user and the inside of the computer.

What is swing in java

Swing is based on the Abstract Window Toolkit (AWT) so that cross-platform applications can use any pluggable appearance style. Swing developers can take advantage of Swing's rich, flexible features and modular components to create elegant user interfaces with only a small amount of code.

All packages in the toolkit are named with swing, such as javax.swing, javax.swing.event.

Steps to create a graphical interface with Swing:

1), import the Swing package

2), select the interface style

3), set the top-level container

4), set the button and label

5), place the component on the container

6), for the component Adding borders

7), handling events

8), assistive technology support

Let’s briefly introduce it below:

Import package

The following statement imports the Swing package

import javax.swing.*;
Copy after login

Most Swing programs use the basic underlying structure and event model of AWT, so two packages need to be imported:

import java.awt.*;
import java.awt.event.*;
Copy after login

If the graphics The interface includes event processing, so you also need to import the event processing package:

import javax.swing.event.*;
Copy after login

Select the interface style

Swing allows you to choose the graphical interface style of the program. Commonly used Java styles are , windows style, etc.

The following code is used to select the graphical interface style. The cross-platform Java interface style is selected here.

try { UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName( )); }
catch (Exception e) { }
Copy after login

Set the top-level container

The graphical interface must have at least one top-level Swing container. The top-level Swing container provides support for other Swing components to draw and handle events on the screen.

Commonly used top-level containers:

JFrame (frame): represents the main program window

JDialog (dialog): Each JDialog object represents a Dialog box, the dialog box belongs to the secondary window

JApplet (small program): displays a small program interface in the browser

A frame includes the border, menu bar, toolbar, and status bar, And the pane that occupies the main part in the middle

The pane can also be regarded as a kind of panel, but it is an integral part of the frame

The component will not be placed directly on the frame, but Place it on several panels, and then place these panels on the pane

Use the getContentPane() function of the frame object to obtain the pane, and then call the add() function of the pane to place the panel

public static void main(String[ ]args){JFrame frame=new JFrame("SwingApplication");
JPanel panel1=new JPanel();
frame.getContentPane().add(panel1,BorderLayout.CENTER);
......//添加其他组件
frame.pack();frame.setVisible(true);
Copy after login

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!