Java教程

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

本文主要是介绍JavaGUI三种布局管理器FlowLayout,BorderLayout,GridLayout的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

三种布局管理器

  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的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!