Introduction to Swing

1. Overview of Swing

  • GUI (Graphical User Interface) provides a graphical interface for the program. Its original design purpose is to build a general-purpose GUI for programmers so that it can run on all platforms, but the basic class AWT (Abstract Window Toolbox) in Java 1.0 This requirement was not met, so Swing appeared, which is an enhanced component of the AWT component, but it cannot completely replace the AWT component. These two components need to appear in a graphical user interface at the same time.

1. Swing features

  • The original AWT components come from the java.awt package. When Java applications containing AWT components are executed on different platforms, the display of the GUI components of each platform will be different, but when running programs developed using Swing on different platforms , you can unify the display style of GUI components, because Swing components allow programmers to specify a unified appearance and style when cross-platform.
  • Swing components are commonly referred to as "lightweight components" because they are written entirely in the Java language, which is an operating system-independent language that can run on any platform; on the contrary, components that depend on the local platform are called Called "heavyweight components", such as AWT components rely on the window system of the local platform to determine the function, appearance and style of the component.
  • Swing mainly has the following characteristics:
    • Lightweight components.
    • Pluggable skin components.

2. Swing package

  • In order to effectively use Swing components, you must understand the structure hierarchy and inheritance relationship of Swing packages, among which the more important classes are Component class, Container class, and JComponent class.
  • The class hierarchy and inheritance relationship of Swing components:
    insert image description here
  • In Swing components, most GUI components are direct subclasses or indirect subclasses of Component class. JComponent class is the storage location of various features of Swing components. The features of these components include setting component boundaries, automatic scrolling of GUI components, etc.
  • The most important Frey in Swing components is the Container class, and the Container class has two most important subclasses, namely java.awt.Window and java.awt.Frame, except that the previous AWT class components will inherit these two class, the current Swing components also extend these two classes. The top-level parent class is the Component class and the Container class, so the writing of window components in Java is related to the concept of components and containers.

3. Overview of common Swing components

component name definition
JButton Represents a Swing button, and the button can have some pictures or text.
JCheckBox Represents a checkbox component in Swing.
JcomBox Represents a drop-down list box, which can display multiple options in the drop-down display area.
JFrame Represents Swing's framework classes.
JDialog Represents the Swing version of the dialog.
JLabel Represents a label component in Swing.
JRadioButton Represents a radio button in Swing.
JList Represents a component capable of displaying a list of items in a user interface.
JTextField Represents a text box.
JPasswordField Represents a password box.
JTextArea Represents a text area in Swing.
JOptionPane Represents some dialog boxes in Swing.

Two, commonly used forms

  • As the carrier of components in Swing applications, it is in a very important position. Forms commonly used in Swing include JFrame and JDialog.

1. JFrame form

  • The JFrame form is a container, which is the carrier of various components in the Swing program, and JFrame can be regarded as a container for carrying these Swing components. When developing an application, you can create a form by inheriting the java.swing.JFrame class, add components to this form, and set events for the components at the same time. Since the form inherits the JFrame class, it has buttons such as "Maximize", "Minimize", and "Close".
  • Syntax of JFrame:
JFrame jf = new JFrame(title);
Contianer container = jf.getContentPane();
// jf:JFrame类的对象。
// container:Container类的对象,可以使用JFrame对象调用getContentPane()方法获取。
  • The form of a Swing component is usually related to components and containers, so after the JFrame object is created, you need to use the getContentPane() method to convert the form into a container, and then add components or set the layout manager in the container. Typically, this container is used to contain and display components. If a component needs to be added to the container, it can be set using the add() method from the Container class.
container.add(new JButton("按钮"));    // JButton组件
  • After adding components to the container, these components can also be removed from the container using the remove() method of the Container class.
container.remove(new JButton("按钮"));     

Example 1 Use JLabel simple label component

  • The following example implements the JFrame object to create a form and add a component to it.
package pack1;
import java.awt.*;    // 导入awt包
import javax.swing.*;   // 导入swing包


public class Example1 extends JFrame {
    
    

	public void CreateJFrame(String title) {
    
    
		JFrame jf = new JFrame(title);       // 实例化一个JFrame对象
		Container container = jf.getContentPane();     // 获取一个容器
		JLabel jl = new JLabel("这是一个JFrame窗体");    // 获取一个JLabel标签
		jl.setHorizontalAlignment(SwingConstants.CENTER);    // 使标签上的文字居中
		
		container.add(jl);     // 将标签添加到容器中
		container.setBackground(Color.white);     // 设置容器颜色背景
		jf.setVisible(true);   // 设置窗体可视
		jf.setSize(200, 150);   // 设置窗体大小
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   // 设置窗体关闭方式
	}
	
	public static void main(String[] args) {
    
        // 再主方法中调用CreateJFrame()方法
		// TODO Auto-generated method stub
		new Example1().CreateJFrame("创建一个JFrame窗体");
	}
}

insert image description here

  • The Example1 class inherits the JFrame class, and the JFrame object is instantiated in the CreateJFrame() method. Common construction methods of the JFrame class include the following two forms:
    • public JFrame()
    • public JFrame(String title)
  • The two construction methods in the JFrame class are the construction methods without parameters and the construction methods with parameters. The first form of construction method can create a new form that is initially invisible and has no title; the second form is when instantiating the JFrame object. It is possible to create an invisible but titled form. You can use the JFrame object to call the show() method to make the form visible, but this method has long been deprecated by the new version of JDK, and the setVisible(true) method is usually used to make the form visible.
  • You can use the setSize(int x, int y) method to set the size of the form, where the x and y variables represent the width and height of the form, respectively.
  • After creating the form, you need to give the form a closing method, you can call the setDefaultCloseOperation() method to close the form. Java provides a variety of ways to close the form, and the following four are commonly used:
    • DO_NOTHING_ON_CLOSE: Close the form without doing anything.
    • DISPOSE_ON_CLOSE: Any registered listener object will automatically hide and release the form.
    • HIDE_ON_CLOSE: The default window for hidden windows is closed.
    • EXIT_ON_CLOSE: Exit the application and the default window is closed.
  • These operations are essentially sealing a constant of type int in the javax.swing.WindowConstants interface.

2. JDialog form

  • The JDialog form is a dialog box in the Swing component, which inherits the java.awt.Dialog class in the AWT component.
  • The function of the JDialog form is to pop up another form from one form, just like popping up a confirmation dialog box when using the IE browser. The JDialog form is essentially another type of form. It is similar to the JFrame form. When using it, you also need to call the getContentPane() method to convert the form into a container, and then set the characteristics of the form in the container.
  • Creating a JDialog form in an application requires instantiating the JDialog class, usually using the following construction methods of the Dialog class
    • public JDialog(): Creates a dialog box without title and parent form.
    • public JDialog(Frame f): Create a dialog box specifying the parent form, but the form has no title.
    • public JDialog(Frame f, boolean model): Create a dialog box of the specified type and specify the parent form, but the form does not specify a title.
    • public JDialog(Frame f, String title): Creates a dialog with the specified title and parent frame.
    • public JDialog(Frame f, String title, boolean model): Creates a dialog with the specified title and the infrared mode of the form.

Guess you like

Origin blog.csdn.net/ungoing/article/details/130768679