Java GridLayout(网格布局)布局管理器

您所在的位置:网站首页 gridlayout超出屏幕 Java GridLayout(网格布局)布局管理器

Java GridLayout(网格布局)布局管理器

2023-09-09 11:36| 来源: 网络整理| 查看: 265

GridLayout(网格布局)

​ GridLayout 布局管理器将容器分割成纵横线分隔的网格 , 每个网格所占的区域大小相同。当向使用 GridLayout 布局管理器的容器中添加组件时, 默认从左向右、 从上向下依次添加到每个网格中 。 与 FlowLayout不同的是,放置在 GridLayout 布局管理器中的各组件的大小由组件所处的区域决定(每 个组件将自动占满整个区域) 。

构造方法方法功能GridLayout(int rows,in t cols)采用指定的行数、列数,以及默认的横向间距、纵向间距将容器 分割成多个网格GridLayout(int rows,int cols,int hgap,int vgap)采用指定 的行数、列 数 ,以及指定的横向间距 、 纵向间距将容器分割成多个网格。

案例:

​ 使用Frame+Panel,配合FlowLayout和GridLayout完成一个计算器效果。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hloEdvRs-1612002352981)(./images/计算器.jpg)]

代码:

import java.awt.*; public class GridLayoutDemo{ public static void main(String[] args) { //1.创建Frame对象,并且标题设置为计算器 Frame frame = new Frame("计算器"); //2.创建一个Panel对象,并且往Panel中放置一个TextField组件 Panel p1 = new Panel(); p1.add(new TextField(30)); //3.把上述的Panel放入到Frame的北侧区域 frame.add(p1,BorderLayout.NORTH); //4.创建一个Panel对象,并且设置其布局管理器为GridLayout Panel p2 = new Panel(); p2.setLayout(new GridLayout(3,5,4,4)); //5.往上述Panel中,放置15个按钮,内容依次是:0,1,2,3,4,5,6,7,8,9,+,-,*,/,. for (int i = 0; i public static void main(String[] args) { //1.创建Frame对象 Frame frame = new Frame("这里是GridBagLayout测试"); //2.创建GridBagLayout对象 GridBagLayout gbl = new GridBagLayout(); //3.把Frame对象的布局管理器设置为GridBagLayout frame.setLayout(gbl); //4.创建GridBagConstraints对象 GridBagConstraints gbc = new GridBagConstraints(); //5.创建容量为10的Button数组 Button[] bs = new Button[10]; //6.遍历数组,初始化每一个Button for (int i = 0; i gridBagLayout.setConstraints(c,gridBagConstraints); container.add(c); } }

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3