GridBagLayout布局管理器

本文介绍GridBagLayout布局管理器的特点及使用方法。GridBagLayout允许组件以不同大小和位置自由排列,通过GridBagConstraints对象定制每个组件的位置和尺寸。示例代码展示了如何创建一个包含多个按钮的面板,并使用GridBagLayout进行布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同即可将组件垂直和水平对齐。
 * 每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为    显示区域。
 * 每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。
 * Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。除了 Constraints 对象之外,
 * GridBagLayout 还考虑每个组件的最小和首选大小,以确定组件的大小。
 * 网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,
 * 网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,
 * 网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。
 * 为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。
 * 可以通过设置一个或多个实例变量来自定义 GridBagConstraints 对象:

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

public class GridBagLayout1 {

    public GridBagLayout1() {
        super();

    }

    public static void main(String[] args) {
        GridBagFrame gf = new GridBagFrame();
        gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gf.setVisible(true);

    }

}

class GridBagFrame extends JFrame {
    GridBagFrame() {
        setTitle("Hello GridBagLayout");
        setSize(400, 200);
        Container c = getContentPane();
        GridBagPanel g = new GridBagPanel();
        c.add(g);

    }

}

class GridBagPanel extends JPanel {
    GridBagPanel() {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        setFont(new Font("SansSerif", Font.PLAIN, 14));
        setLayout(gridbag);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        makebutton("Button1", gridbag, c);
        makebutton("Button2", gridbag, c);
        makebutton("Button3", gridbag, c);

        c.gridwidth = GridBagConstraints.REMAINDER; // end row
        makebutton("Button4", gridbag, c);

        c.weightx = 0.0; // reset to the default
        makebutton("Button5", gridbag, c); // another row

        c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last in row
        makebutton("Button6", gridbag, c);

        c.gridwidth = GridBagConstraints.REMAINDER; // end row
        makebutton("Button7", gridbag, c);

        c.gridwidth = 1; // reset to the default
        c.gridheight = 2;
        c.weighty = 1.0;
        makebutton("Button8", gridbag, c);

        c.weighty = 0.0; // reset to the default
        c.gridwidth = GridBagConstraints.REMAINDER; // end row
        c.gridheight = 1; // reset to the default
        makebutton("Button9", gridbag, c);
        makebutton("Button10", gridbag, c);

    }
    protected void makebutton(String name, GridBagLayout gridbag,
            GridBagConstraints c) {
        Button button = new Button(name);
        gridbag.setConstraints(button, c);
        add(button);
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值