【GUI】布局管理器


1、流式布局
package com.hyr.lesson01;

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

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //按钮组件
        Button button1 = new Button("下一步");
        Button button2 = new Button("取消");
        Button button3 = new Button("确定");

        //设置为流式布局
        //frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setBounds(200, 200, 200, 200);
        frame.setBackground(new Color(94, 168, 241));
        frame.setResizable(false);

        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

2、东南西北中
package com.hyr.lesson01;

import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button east = new Button("东");
        Button west = new Button("西");
        Button south = new Button("南");
        Button north = new Button("北");
        Button center = new Button("中");

        frame.add(east, BorderLayout.EAST);
        frame.add(west, BorderLayout.WEST);
        frame.add(south, BorderLayout.SOUTH);
        frame.add(north, BorderLayout.NORTH);
        frame.add(center, BorderLayout.CENTER);

        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(new Color(97, 220, 103));
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });


    }
}

3、表格布局
package com.hyr.lesson01;

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

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button button1 = new Button("东");
        Button button2 = new Button("西");
        Button button3 = new Button("南");
        Button button4 = new Button("北");
        Button button5 = new Button("中");

        frame.setLayout(new GridLayout(3, 2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);

        frame.pack();//java函数

        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(new Color(97, 220, 103));
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });

    }
}
GUI