3月19日,GridBagLayoutDemo,每日20行。

import java.awt.*;
public class GridBagLayoutDemo {

    public static void main(String[] args) {
        Frame f = new Frame("GridBagLayout Sample");
        f.setSize(320, 180);    //设置窗口的大小
        GridBagConstraints gbc = new GridBagConstraints();    //使用类GridBagConstraints
        f.setLayout(new GridBagLayout());    //设定外观管理器位GridBagLayout外观管理器
        gbc.fill = GridBagConstraints.BOTH;    //所有的按钮都会把分配的剩余空间填满
        gbc.gridheight = 1;    //设置第一个按钮显示属性
        gbc.gridwidth = 1;
        Button button1 = new Button("东");
        ((GridBagLayout)f.getLayout()).setConstraints(button1, gbc);
        f.add(button1);
        gbc.gridwidth = GridBagConstraints.REMAINDER;    //设置第二个按钮的gridwidth,gridheigth保持不变
        Button button2 = new Button("西");
        ((GridBagLayout)f.getLayout()).setConstraints(button2, gbc);
        f.add(button2);
        gbc.gridheight = 4;    //设置第三个按钮显示属性
        gbc.gridwidth = 1;
        Button button3 = new Button("南");
        ((GridBagLayout)f.getLayout()).setConstraints(button3, gbc);
        f.add(button3);
        gbc.gridheight = 2;    //设置第四个按钮显示属性
        gbc.gridwidth = 1;
        Button button4 = new Button("北");
        ((GridBagLayout)f.getLayout()).setConstraints(button4, gbc);
        f.add(button4);
        gbc.gridwidth = GridBagConstraints.REMAINDER;    //设置第五个按钮显示属性
        Button button5 = new Button("中");
        ((GridBagLayout)f.getLayout()).setConstraints(button5, gbc);
        f.add(button5);
        gbc.insets = new Insets(5, 6, 7, 8);    //设置第六个按钮显示属性
        Button button6 = new Button("GridBagLayoutDemo");
        ((GridBagLayout)f.getLayout()).setConstraints(button6, gbc);
        f.add(button6);
        f.setVisible(true);
    }

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java的GridBagLayout布局管理器非常灵活,可以实现各种布局。对于三两列的布局,可以按照以下步骤进实现: 1. 创建一个JPanel面板,将其布局管理器设置为GridBagLayout。 2. 创建一个GridBagConstraints对象,用于控制组件的布局。 3. 创建需要布局的组件,并设置它们的位置和大小。 4. 使用GridBagConstraints对象来设置组件的位置和大小,并将组件添加到面板中。 下面是一个简单的示例代码,实现了一个三两列的布局: ``` import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class GridBagLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayout Demo"); JPanel panel = new JPanel(new GridBagLayout()); // 创建三个按钮 JButton btn1 = new JButton("Button 1"); JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); // 创建GridBagConstraints对象 GridBagConstraints gbc = new GridBagConstraints(); // 设置第一 gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; panel.add(btn1, gbc); gbc.gridx = 1; panel.add(btn2, gbc); // 设置第二 gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; panel.add(btn3, gbc); // 设置第三 gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; panel.add(new JButton("Button 4"), gbc); gbc.gridx = 1; panel.add(new JButton("Button 5"), gbc); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ``` 在上面的代码中,我们创建了一个JPanel面板,并将其布局管理器设置为GridBagLayout。然后创建了三个按钮,并使用GridBagConstraints对象来设置它们的位置和大小。最后将这些组件添加到面板中,并将面板添加到窗口中。运程序后,可以看到一个三两列的布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值