首页 > 编程语言 > 详细

JavaGUI三种布局管理器FlowLayout,BorderLayout,GridLayout的使用

时间:2021-07-15 17:24:48      阅读:25      评论:0      收藏:0      [点我收藏+]

三种布局管理器

  1. 流式布局FlowLayout
package GUI;

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("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        // 设置为流式布局,默认居中
        // frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // 将按钮添加到窗体中
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        // 设置窗体可见性,大小
        frame.setVisible(true);
        frame.setSize(500,500);

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

  1. 东西南北中BorderLayout
package GUI;

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("TestBorderLayout");

        // 实例化按钮
        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

        // 将按钮添加到窗体中,设置为边界布局格式
        // frame.setLayout(new BorderLayout());
        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.setVisible(true);
        frame.setSize(500,500);

        // 监听窗口关闭事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

  1. 表格布局GridLayout
package GUI;

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("TestGridLayout");

        // 实例化按钮
        Button bt1 = new Button("bt1");
        Button bt2 = new Button("bt2");
        Button bt3 = new Button("bt3");
        Button bt4 = new Button("bt4");
        Button bt5 = new Button("bt5");
        Button bt6 = new Button("bt6");

        // 设置表格布局
        frame.setLayout(new GridLayout(3,2));

        // 将按钮添加到窗口中
        frame.add(bt1);
        frame.add(bt2);
        frame.add(bt3);
        frame.add(bt4);
        frame.add(bt5);
        frame.add(bt6);

        // 设置窗口可见性,大小/使用自动大小配置
        frame.setVisible(true);
        frame.pack();  // Java函数自动匹配合适大小

        // 监听窗口关闭事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

JavaGUI三种布局管理器FlowLayout,BorderLayout,GridLayout的使用

原文:https://www.cnblogs.com/CH0701/p/15015951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!