javaswing-GridLayout九宫格布局管理器

在这里插入图片描述

GridLayout使用步骤

  1. 创建一个九宫格布局管理器(设置好几行几列)
  2. 将一个容器设置为该布局
package GuiLayout;

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

public class GridLayoutDemo {
    static final int WIDTH=300;
    static final int HEIGHT=200;
    public static void main(String[] args) {
        JFrame jf = new JFrame("**GridLayout布局管理器**");
        jf.setSize(WIDTH,HEIGHT);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);

        JPanel contentPane = new JPanel();
        jf.setContentPane(contentPane);

        JButton c1 = new JButton("白菜");
        JButton c2 = new JButton("胡萝卜");
        JButton c3 = new JButton("青菜");
        JButton c4 = new JButton("西红柿");
        JButton c5 = new JButton("土豆");
        JButton c6 = new JButton("白萝卜");
        JButton c7 = new JButton("青菜");
        JButton c8 = new JButton("西红柿");
        JButton c9 = new JButton("土豆");
		
        GridLayout gird = new GridLayout(3, 3);
        //1,创建一个GridLayout布局管理器
        //2,将其行列数设置为3,3,并且将之作为中间容器的布局管理器
        contentPane.setLayout(gird);
        contentPane.add(c1);
        contentPane.add(c2);
        contentPane.add(c3);
        contentPane.add(c4);
        contentPane.add(c5);
        contentPane.add(c6);
        contentPane.add(c7);
        contentPane.add(c8);
        contentPane.add(c9);

        jf.pack();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值