layout的简单练习

package com.sean.layout;

import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @create 2021-03-02 10:40
 */
public class Homework01 {
    public static void main(String[] args) {

        Frame frame = new Frame("Layout-Homework");

        Panel panel1 = new Panel();
        Panel panel2 = new Panel();

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        Button button7 = new Button("button7");
        Button button8 = new Button("button8");
        Button button9 = new Button("button8");
        Button button10 = new Button("button8");
        Button button11 = new Button("button8");

//整体架构2行3列
        frame.setLayout(new GridLayout(2,3));
//一个格子里装上下2格
        panel1.add(button6,BorderLayout.NORTH);
        panel1.add(button7,BorderLayout.SOUTH);
        panel1.setLayout(new GridLayout(2,1));
        panel1.setBounds(50,0,50,50);

//一个格子里装四个
        panel2.add(button8);
        panel2.add(button9);
        panel2.add(button10);
        panel2.add(button11);
        panel2.setLayout(new GridLayout(2,2));
        panel2.setBounds(150,150,50,50);
//把格子拼装起来,按顺序,从左到右
        frame.add(button1);
        frame.add(panel1);
        frame.add(button5);
        frame.add(button2);
        frame.add(panel2);
        frame.add(button3);



//可视化
        frame.pack();
        frame.setVisible(true);

//关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });





    }
}

在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页