Introduction to Java Swing

Introduction to UI components

Before you start learning Swing, you must answer a question for real beginners: What is UI? The answer for beginners is "user interface". But because the goal of this tutorial is to ensure that you are no longer just a beginner, we need a more advanced definition than this one.

So, I ask this question again: What is UI? You may define it as the button you press, the address bar for typing, the window opened and closed, etc. These are all UI elements, but in addition to those you see on the screen, there are more Are UI elements. Such as the mouse, keyboard, volume, screen color, fonts used, and the position of an object relative to another object are all included in the UI. In fact, any object that plays a role in the interaction between the computer and the user is an integral part of the UI. This seems simple enough, but you should be surprised that many people and large companies have been working on it for many years. In fact, the only course of some university majors is to study this kind of interaction.
Swing is a development kit used to develop the user interface of Java applications. It provides a visual interactive interface for Java, allowing programs to run without the console.

Now we look at the specific content in the Swing package

JFrame:代表一个界面的窗口,也就是一个程序的门面,我们在创建图形化界面的时候必须要首先创建这个窗口

JLabel:这是一个标签组件,仅仅用于显示文字,图像等内容。这是Swing最简单的一个组件。

JBtton:这是按钮组件,我们可以为其添加事件。

JTextField:这是一个单行文本框,用于接收输入的文本
JPasswordField:这是一个密码文本框

Jpanel:这是一个面板组件,相当于一个小容器

Next, let’s take a look at what Java Swing looks like through a simple "HelloWorld" code.

package swing;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class HelloWorldSwing {
    
    
    /**{
     * 创建并显示GUI。出于线程安全的考虑,
     * 这个方法在事件调用线程中调用。
     */
private static JFrame frame;
private static JLabel label;
    private static void createAndShowGUI() {

         // 创建及设置窗口
        frame = new JFrame("HelloWorldSwing");
//创建容器
        Container c=frame.getContentPane();
        // 添加 "Hello World" 标签
        label = new JLabel("Hello World");

        //设置标签居中显示
        label.setHorizontalAlignment(SwingConstants.CENTER);
        c.add(label);

        // 显示窗口
        frame.setSize(500,500);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
       createAndShowGUI();
    }
}

As you can see from the above code, we first instantiate a window, and then get a Container class through the getContentPane() method

We should have such a concept that the window of a Swing component is usually related to the component and the container, so after the JFrame window object is created, we need to call the getContentPane() method to convert the window into a container, and then add various components to the container and Set the layout. If you need to add a component to the container, we can use the add() method in the Container class to add it.
For example, the above code:

c.add(label);

In this code, we add our label component to the container through the add() method.

其实这个Swing无非就是将一个个组件添加到容器中去,设置组件的属性(对齐方式啊,颜色啊,大小啊),为组件添加响应事件(按了这个按钮会怎样,离开这个文本框会怎样等等),设置布局管理器(就是设置各个组件的排列方式)。

Next, we modify the above program. We add a button to the program. After pressing this button, the text displayed on the interface will change to "Hello, world"

public class HelloWorldSwing {
    
    
    /**{
     * 创建并显示GUI。出于线程安全的考虑,
     * 这个方法在事件调用线程中调用。
     */
    private static JFrame frame;
    private static JLabel label;
    private static JPanel jp1;
    private static JPanel jp2;
    private static Button change;
    private static void createAndShowGUI() {

       // 创建及设置窗口
         frame = new JFrame("Hello World");
        Container c=frame.getContentPane();
        // 添加 "Hello World" 标签
        label = new JLabel("Hello World");

        //实例化按钮组件
        change=new Button("change");

        //为按钮添加响应时间
        change.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                //改变标签中的文字
                label.setText("你好,世界");
            }
        });
        //设置居中显示
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //实例化面板
        jp1=new JPanel();
        jp2=new JPanel();

        //向面板中添加内容
        jp1.add(label);
        jp2.add(change);

        //向容器中添加内容
        c.add(BorderLayout.NORTH,jp1);
        c.add(BorderLayout.SOUTH,jp2);

        // 显示窗口
        frame.setSize(500,500);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
       createAndShowGUI();
    }
}

We can see that in the above code we have added two JPanel panels. This is to separate the "label" and "button". Then we created a button component and added a listener event to it. The content is to change the text in the label. Next, we will add "label" and "button" to two panels respectively, and add the panels to the container according to the layout of "BorderLayout"


Stress again

Swing is nothing more than adding each component to the container, setting the component properties (alignment, color, size), adding response events to the component (what happens when you press this button, what happens when you leave the text box, etc. ), set the layout manager (that is, set the arrangement of each component).

Ok, after this concept we can learn Swing happily

Guess you like

Origin blog.csdn.net/mrliqifeng/article/details/73610397