[Use AWT to develop GUI programs] Use the basic components of AWT to perform various operations

[Use AWT to develop GUI programs] Use the basic components of AWT to perform various operations

package com.hk.client.login;

import javax.swing.*;
import java.awt.*;

/**
 * 使用 AWT 的基本组件进行各类操作
 *
 * @author: shipleyleo
 * @create: 2023-06-05 18:19:13
 */
public class Yongzu {
    
    
    Frame f = new Frame("Test");
    // 按钮
    Button ok = new Button("Confirm");
    // 单选框
    CheckboxGroup cbg = new CheckboxGroup();
    Checkbox male = new Checkbox("Male", cbg, true); // 定义一个单选框(处于cbg一组),初始处于被选中状态(true)
    Checkbox female = new Checkbox("Female", cbg, false); // 定义一个单选框(处于cbg一组),初始处于没有选中状态(false)
    // 复选框,初始处于没有选中状态(false)
    Checkbox married = new Checkbox("Married or not? ", false);
    // 下拉选择框
    Choice colorChooser = new Choice();
    // 列表选择框
    List colorList = new List(6, true);
    // 多行文本框,定义一个5行20列的多行文本框
    TextArea ta = new TextArea(5, 20);
    // 单行文本框,定义一个50列的单行文本框
    TextField name = new TextField(50);

    public void init() {
    
    
        colorChooser.add("Red");
        colorChooser.add("Green");
        colorChooser.add("Blue");
        colorList.add("Red");
        colorList.add("Green");
        colorList.add("Blue");

        // 创建一个装载了文本框、按钮的 Panel
        Panel bottom = new Panel();
        bottom.add(name); // 单行文本框:TextField
        bottom.add(ok); // 按钮:Button
        f.add(bottom, BorderLayout.SOUTH);

        // 创建一个装载了下拉选择框、三个 Checkbox 的 Panel
        Panel checkPanel = new Panel();
        checkPanel.add(colorChooser); // 下拉选择框:Choice
        checkPanel.add(male); // 单选框:Checkbox 搭配 CheckboxGroup
        checkPanel.add(female); // 单选框:Checkbox 搭配 CheckboxGroup
        checkPanel.add(married); // 复选框:Checkbox
        // 垂直排列。创建一个垂直排列组件的 Box,盛装多行文本框、Panel
        Box topLeft = Box.createVerticalBox();
        topLeft.add(ta); // 多行文本框:TextArea
        topLeft.add(checkPanel);

        // 水平排列。创建一个水平排列组件的 Box,盛装 topLeft、colorList
        Box top = Box.createHorizontalBox();
        top.add(topLeft);
        top.add(colorList); // 列表选择框:List
        // 将 top Box 容器添加到窗口的中间
        f.add(top);

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

    public static void main(String[] args) {
    
    
        new Yongzu().init();
    }

}

Output effect:

insert image description here

Corresponding component analysis:


insert image description here

Guess you like

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