Java graphical interface programming AWT and Swing learning record and sharing (a container container)

Java uses AWT and Swing-related classes to complete graphical interface programming. The full name of AWT is Abstract Window Toolkit (Abstract Window Toolkit), which is the earliest GUI library provided by Sun. This GUI library provides some basic functions, but this The functions of the GUI library are relatively limited, so sun later provided the Swing library. By using the graphical interface component library provided by AWT and Swing,
the basic inheritance relationship will not be repeated here.

component's API

First of all, we know that container inherits and component. Its characteristic is that it can be used as a component and it can also accommodate other components.
List of Inheritance Relationships
List of Inheritance Relationships

We can generally understand that the window is just a canvas and the container is the part we decorate (if there is any mistake in personal understanding, please help to correct it)

insert image description here
Here's a demonstration:

package study;

import java.awt.*;

public class study {
    
    
    public static void main(String[] args) {
    
    
        Frame test=new Frame("这里测试window窗口");
        //设置位置,大小
        test.setLocation(100,100);
        test.setSize(500,300);
        //使之可视化
        test.setVisible(true);

    }
}
```![运行的结果](https://img-blog.csdnimg.cn/32102af8de3d410fb20ca809b6ed3e4e.png)
# 开始添加文本域和按钮

```java
package study;

import java.awt.*;

public class study {
    
    
    public static void main(String[] args) {
    
    
        Frame test=new Frame("这里接着测试window窗口");
        //设置位置,大小
        test.setLocation(100,100);
        test.setSize(500,300);
        //创建内嵌容器对象
        Panel p=new Panel();
        //创建文本域和按钮同时添加进去
        p.add(new TextField("现在进行测试"));
        p.add(new Button("按下去吧"));
        //再将容器添加到窗口上
        test.add(p);
        //使之可视化
        test.setVisible(true);

    }
}

insert image description here

ScrollPane container (with scroll bars)

package study;

import java.awt.*;

public class study {
    
    
    public static void main(String[] args) {
    
    
        Frame test=new Frame("这里演示scrollPane容器");
        //设置位置,大小
        test.setBounds(100,200,1000,500);
        //创建scrollPane容器对象
        ScrollPane sp=new ScrollPane();
        //创建文本域和按钮同时添加进去
        sp.add(new TextField("现在进行测试"));
        sp.add(new Button("按下去吧"));
        //再将容器添加到窗口上
        test.add(sp);
        //使之可视化
        test.setVisible(true);

    }
}

insert image description here
Careful readers will find that there is obviously no scroll bar here. The reason is that the text content I entered is not worth appearing. The content of the scroll bar is too small, so how to force it to
appear? See the following code

insert image description here
after thisMore scroll bars on both sides

So today's content is here first, and if I update it later, I will attach the corresponding link to this article. Thank you!

Guess you like

Origin blog.csdn.net/qq_74415153/article/details/131325809