0. Introduction to Swing

Swing is a set of toolkits provided by Java for the development of graphical interface applications, and is a part of the basic Java classes.

Swing includes various components for building graphical interfaces (GUI), such as windows, labels, buttons, text boxes, etc.

Swing provides many screen display elements better than AWT, implemented in pure Java, and can be better compatible with cross-platform operation.

In order to distinguish it from AWT components, Swing components are under the javax.swing.* package, and the class names all start with J, such as: JFrame, JLabel, JButton, etc.

Swing components

A Java graphical interface, from a variety of different types of "elements", for instance: window, menu bars, dialog boxes, labels, buttons, text boxes, and so on, these "elements" unity is called component (the Component) .

Components according to different functions can be divided into top container, the intermediate container, the basic components . The composition of a simple window is shown in the following hierarchical structure:

 

Inheritance of component types:

  • The top-level container belongs to the window class component, inherited from java.awt.Window;
  • The intermediate container and basic components inherit from javax.swing.JComponent.

2.1 Top container

The top-level container is a window component and can be displayed independently. A graphical interface requires at least one window, for example:

 

2.2 Intermediate container

The intermediate container serves as the carrier of the basic components and cannot be displayed independently. The intermediate container can add several basic components (or nested intermediate containers) to manage the components in the container, similar to grouping and managing various complex components. The topmost intermediate container must rest in the top container (window).

 

Commonly used intermediate containers (panels):

 

Special intermediate container:

 

2.3 Basic components

The basic component is the component that directly realizes the human-computer interaction.

 

Commonly used simple basic components:

 

Picker component:

 

Other more complex basic components:

 

Layout manager

To add various components of Swing (JComponent) to the panel container (JPanel), you need to specify a layout manager (LayoutManager) for the panel container, and clarify the arrangement of the components in the container (Container).

 

Commonly used layout managers:

 

 Getting started case

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestJFrame {

	public static void main(String[] args) {

		//创建一个顶层容器
		JFrame jf = new JFrame();
		//设置大小
		jf.setSize(400, 300);
		//设置位置 居中
		jf.setLocationRelativeTo(null);//基于当前窗口居中,null默认表示当前系统
		
		//手动关闭窗口时未结束程序
		//需要设置关闭时退出JVM
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置标题
		jf.setTitle("第一个Swing窗口");
		
		//设置窗口不能最大化且不能缩放窗口
		jf.setResizable(false);
		
		//创建一个中间容器
		JPanel panel = new JPanel();
		//创建一个按钮组件
		JButton btn = new JButton("我是第一个个按钮");
		JButton btn2 = new JButton("我是第二个个按钮");
		//将按钮添加到panel中
		panel.add(btn);
		panel.add(btn2);
		//将中间容器panel添加到窗口中
		jf.setContentPane(panel);
		
		//显示窗口
		jf.setVisible(true);
	}

}

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/109017092