Java Swing Quick Start

Java Swing Quick Start

1. A brief introduction to Java Swing

1. Introduction to Java Swing


Swing is a GUI toolkit designed for Java. (Swing is a development toolkit for developing user interfaces for Java applications.)


Swing is part of the Java Foundation Classes.


Swing includes graphical user interface (GUI) components such as: text boxes, buttons, split panes and tables.


Swing provides many better screen display elements than AWT. They're written in pure Java, so they're as cross-platform as Java itself, unlike AWT. They are part of JFC. They support replaceable panels and themes (default themes unique to various operating systems), but instead of really using the devices provided by the native platform, they only mimic them superficially. This means you can use any panel supported by JAVA on any platform. The disadvantage of lightweight components is slower execution speed, and the advantage is that they can adopt uniform behavior on all platforms.

2. Java Swing usage steps

(1) Import the Swing package
(2) Select the interface style
(3) Set the top-level container
(4) Set the button and label
(5) Put the component on the container
(6) Add a border to the component
(7) Handle the event
(8) Assist Technical Support



Import
The following statement imports the Swing package

import javax.swing.*;

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.*;

If event processing is included in the graphical interface, the event processing package also needs to be imported:

import javax.swing.event.*;


Select the interface style
Swing allows you to select the graphical interface style of the program. Commonly used are java style, windows style and so on.
The following code is used to select the graphical interface style, and the cross-platform Java interface style is selected here.

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


Setting the top-level container
A graphical interface must have at least one top-level Swing container.
The top-level Swing container provides support for other Swing components to draw on the screen and handle events.
Commonly used top-level containers:
JFrame(框架): represents the main program window
JDialog(对话框): each JDialog object represents a dialog box, and the dialog box belongs to the secondary window
JApplet(小程序): displays a small program interface in the browser.
A frame includes borders, menu bars, toolbars, and status bars. And the pane in the middle, which is the main part
, can also be regarded as a panel, but it is a part of the frame. Components are
not placed directly on the frame, but on several panels, and these panels are placed on the 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);
  ......//


add components

  frame.pack();
  frame.setVisible(true);
}

3. Java Swing components

insert image description here

insert image description here

2. Java Swing use cases

(1) Simple use case of Java Swing

1. Hello World program

HelloWorldSwing.java

import javax.swing.*;

/**
 * 一、Hello World 程序
 * 
 * @author: shipleyleo
 * @create: 2023-05-25 14:51:41
 */
public class HelloWorldSwing {
   
    
    

    private static void createAndShowGUI() {
   
    
    
        JFrame.setDefaultLookAndFeelDecorated(true);
        // 创建及设置窗口
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 添加标签
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);
        // 显示窗口
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
   
    
    
        // 显示应用 GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
   
    
    
            @Override
            public void run() {
   
    
    
                createAndShowGUI();
            }
        });
    }
}

The result after compiling and running is shown in the figure below:
insert image description here

2. An example of a user login box

SwingLoginExample.java

import javax.swing.*;

/**
 * 二、一个用户登录框实例
 *
 * @author: shipleyleo
 * @create: 2023-05-25 15:08:33
 */
public class SwingLoginExample {
   
    
    

    public static void main(String[]

Guess you like

Origin blog.csdn.net/Shipley_Leo/article/details/130866972