GridBagLayout和GridBagConstraints的可配置项

GridBagLayout的可配置项

属性 功能
columnWidths 用int数组设置列的宽度, 0代表该列自动宽度 , 数组length并不代表列数
rowHeights 用int数组设置行的高度, 0代表该行自动高度 , 数组length并不代表行数
columnWeights 此字段包含对列权重的覆盖。 如果此字段不为空,则在计算所有列权重后将值应用于网格包。 如果 columnWeights[i] > 列 i 的权重,则为列 i 分配 columnWeights[i] 中的权重。 如果 columnWeights 的元素多于列数,则忽略多余的元素 - 它们不会导致创建更多列。 单位是以整个容器的宽度为1, 当只有一列时 , 1代表宽度占满容器 , 当有多列时 , 不会缩小其它列 . 最小也不会压缩子元素 , 所以取值基本是 0至1的浮点数
rowWeights 此字段保存对行权重的覆盖。 如果此字段不为空,则在计算所有行权重后将值应用于网格包。 如果 rowWeights[i] > 第 i 行的权重,则第 i 行被赋予 rowWeights[i] 中的权重。 如果 rowWeights 的元素多于行数,则忽略多余的元素 - 它们不会导致创建更多行。
  • 用 columnWidths 和 rowHeights 指定的是像素大小, 拖拽容器大小时, 不会跟随变化 , 可以指定大于容器的尺寸
  • 用 columnWeights 和 rowWeights 指定的时相对容器的大小, 会随容器等比缩放, 不能设置大于容器的尺寸
  • 可以4项都设置, 也可这4项都不设置
  • 可以只设置weight, 而不设置width.height

GridBagConstraints的可配置项

    public GridBagConstraints () {gridx = RELATIVE;gridy = RELATIVE;gridwidth = 1;gridheight = 1;weightx = 0;weighty = 0;anchor = CENTER;fill = NONE;insets = new Insets(0, 0, 0, 0);ipadx = 0;ipady = 0;}
    public GridBagConstraints(int gridx, int gridy,int gridwidth, int gridheight,double weightx, double weighty,int anchor, int fill,Insets insets, int ipadx, int ipady) {this.gridx = gridx;this.gridy = gridy;this.gridwidth = gridwidth;this.gridheight = gridheight;this.fill = fill;this.ipadx = ipadx;this.ipady = ipady;this.insets = insets;this.anchor  = anchor;this.weightx = weightx;this.weighty = weighty;}

两者都不配置 , 不会换行

GridBagLayoutConstraints两者都不配置的默认2206100909

代码

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;import javax.swing.*;
public class GridBagLayoutConstraints两者都不配置的默认2206100909 {static Frame frame = new Frame(Thread.currentThread().getStackTrace()[1].getClassName());static GridBagLayout gbl = new GridBagLayout();static ArrayList<JLabel> lbAl = new ArrayList<>();static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setBounds(100,50,1024,768); frame.setLayout(gbl);for(int r=0; r<10; r++)for(int c=0; c<10; c++) {JLabel jlb = new JLabel("JLabel-C"+c+"R"+r); jlb.setOpaque(true); jlb.setBackground(new Color(   (int) (Math.random()*256*256*256+1000000)  ));jlb.setFont(new Font(null, Font.ITALIC, 20) );frame.add(jlb);}}public static void main(String...arguments) {frame.setVisible(true);}}

效果

只配置GridBagLayout , 也不会换行

只配置columnWidths 和 rowHeights

都是是0

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;import javax.swing.*;
public class 单独设置GridBagLayout {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static GridBagLayout gbl = new GridBagLayout();static ArrayList<JLabel> lbAl = new ArrayList<>();static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setBounds(100,50, 1024 , 768); frame.setLayout(gbl);gbl.columnWidths = new int[] {0, 0, 0};
//      gbl.columnWeights = new double[] {0.2 , 0.3 , 0.5 };gbl.rowHeights = new int[] { 0, 0, 0};
//      gbl.rowWeights = new double[] {0.5 , 0.2, 0.3 };for(int r=0; r<1; r++)for(int c=0; c<3; c++) {JLabel jlb = new JLabel("JLabel-C"+c+"R"+r); jlb.setOpaque(true); jlb.setBackground(new Color(   (int) (Math.random()*256*256)  ));jlb.setFont(new Font(null, Font.ITALIC, 20) );frame.add(jlb);}}public static void main(String...arguments) {frame.setVisible(true);}}

columnWidths = new int[] {100, 200, 300};

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;import javax.swing.*;
public class 单独设置GridBagLayout {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static GridBagLayout gbl = new GridBagLayout();static ArrayList<JLabel> lbAl = new ArrayList<>();static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setBounds(100,50, 1024 , 768); frame.setLayout(gbl);gbl.columnWidths = new int[] {100, 200, 300};
//      gbl.columnWeights = new double[] {0.2 , 0.3 , 0.5 };gbl.rowHeights = new int[] { 0, 0, 0};
//      gbl.rowWeights = new double[] {0.5 , 0.2, 0.3 };for(int r=0; r<1; r++)for(int c=0; c<3; c++) {JLabel jlb = new JLabel("JLabel-C"+c+"R"+r); jlb.setOpaque(true); jlb.setBackground(new Color(   (int) (Math.random()*256*256)  ));jlb.setFont(new Font(null, Font.ITALIC, 20) );frame.add(jlb);}}public static void main(String...arguments) {frame.setVisible(true);}}

columnWidths = new int[] {0, 800, 200};

6个标签

10个标签

100个标签


只配置GridBagConstraints

单独设置GridBagConstraints2206111536

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraints2206111536 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=c; gbc.gridy=r;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);}
}

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraints2206111536 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=c; gbc.gridy=r;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);}
}

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraints2206111536 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);if(r==2 || c==3)jb.setText(jb.getText()+"-延长看看");gbc.gridx=c; gbc.gridy=r;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);}
}

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraints2206111536 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=c; gbc.gridy=r;if(c==2)gbc.gridwidth=2;frame.add(jb, gbc);gbc.gridwidth=1;}}public static  void main(String...arguments) {frame.setVisible(true);}
}

if(c==2)gbc.gridwidth=2; 将第三列的gridwidth设为2, 发现看不到第4列, 也看不到C2第三列双倍宽度, 因为列宽没有基准, 设置列宽基准要用GridLayout的属性


用GridLayout的columnWidths 给列宽设置一个基准

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gridWidthHeight需不需要设置GridBagLayout的网格数2206130658 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setBounds(100, 100, 1024, 768);GridBagLayout gbl = new GridBagLayout();gbl.columnWidths = new int[] {100 , 100 , 100 , 100 , 100};
//      gbl.columnWidths = new int[] {100 , 110 , 120 , 130 , 140 , 150 };frame.setLayout(gbl); System.out.println(frame.getLayout());GridBagConstraints gbc = new GridBagConstraints();gbc.anchor = GridBagConstraints.WEST;gbc.fill = GridBagConstraints.HORIZONTAL;
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=c; gbc.gridy=r;if(c==2)gbc.gridwidth=2;if(c==3)continue;frame.add(jb, gbc);gbc.gridwidth=1;}}public static  void main(String...arguments) {frame.setVisible(true);}
}


C2第三列有双倍宽度了
单用GridLayout 的 columnWeights 也可以确定基准

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gridWidthHeight需不需要设置GridBagLayout的网格数2206141448 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setBounds(100, 100, 1024, 768);GridBagLayout gbl = new GridBagLayout();gbl.columnWeights = new double[] {0.1, 0.1 , 0.1  , 0.1 , 0.1};frame.setLayout(gbl); System.out.println(frame.getLayout());GridBagConstraints gbc = new GridBagConstraints();gbc.anchor = GridBagConstraints.WEST;gbc.fill = GridBagConstraints.HORIZONTAL;
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=c; gbc.gridy=r;if(c==2)gbc.gridwidth=2;if(c==3)continue;frame.add(jb, gbc);gbc.gridwidth=1;}}public static  void main(String...arguments) {frame.setVisible(true);}
}

如果多次在一个位置添加(相同的gridx和gridy), 则第一个在最前

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraintsGridxGridy始终为0 {static Frame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=0; gbc.gridy=0;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);
//      frame.remove(0);}
}


如果移除第一个元素 frame.remove(0); 则没有了 Jframe要getContentPane().remove(0)

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraintsGridxGridy始终为0 {static JFrame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=0; gbc.gridy=0;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);frame.getContentPane().remove(0);}
}

gridx和gridy的默认值是 GridBagConstraints.RELATIVE 也就是 -1

package gridBagLayoutConstraints;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 单独设置GridBagConstraintsGridxGridy始终为负一 {static JFrame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());static {frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});frame.setLayout(new GridBagLayout()); frame.setBounds(100, 100, 1024, 768);GridBagConstraints gbc = new GridBagConstraints();
//      gbc.insets.left=6; gbc.insets.top=6;for(int r=0; r<5; r++)for(int c=0; c<5; c++) {JButton jb = new JButton("R"+r+"C"+c);gbc.gridx=-1; gbc.gridy=GridBagConstraints.RELATIVE;frame.add(jb, gbc);}}public static  void main(String...arguments) {frame.setVisible(true);frame.getContentPane().remove(0);}
}

JavaAwtSwing布局 GridBagLayout和GridBagConstraints测试-220610相关推荐

  1. GridBagLayout和GridBagConstraints

    自己想做个小程序,却在布局上犯了难,使用FlowLayout和BorderLayout这些功能不够强大,使用GridBagLayout却不会,只好求助于文档了. 文档对这个布局管理器介绍很详细,但是最 ...

  2. java 布局强制转换_[转]JAVA布局模式:GridBagConstraints终极技巧

    最近正在 修改<公交线路查询系统>,做系统的时候都是用NULL布局,由于NULL布局调用windows系统的API,所以生成的程序无法在其他平台上应用,而 且如果控件的数量很多,管理起来也 ...

  3. JAVA布局模式:GridBagConstraints

    布局模式 :GridBagConstraints布局,先发一个实例: gridx = 2;  // X2 gridy = 0;  // Y0 gridwidth = 1;  // 横占一个单元格 gr ...

  4. 控件获取图像可从几方面取得?_基于图像特征与布局刻画的移动测试脚本跨平台录制回放...

    一. 引言 移动应用在全球范围内有着越发举足轻重的地位,因此移动应用的快速迭代和频繁的需求变更的特点引发了对应用质量保障的要求不断提高.在大型设备集群上迁移测试脚本是移动应用质量保障的关键技术之一,因 ...

  5. 【Java AWT 图形界面编程】LayoutManager 布局管理器 ④ ( GridLayout 网格布局 | GridBagLayout 网格包布局 )

    文章目录 一.GridLayout 网格布局 二.GridLayout 构造函数 三.GridLayout 网格布局代码示例 四.GridBagLayout 网格包布局 一.GridLayout 网格 ...

  6. JAVA布局模式:GridBagConstraints终极技巧

    GridBagConstraints布局,先发一个实例: gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight ...

  7. GridBagLayout 以及 GridBagConstraints 用

    GridBagLayout是一个灵活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干个参数,解释如下: gridx/gridy:组件的横纵坐标 gridwidth ...

  8. GridBagLayout 以及 GridBagConstraints 用法

    GridBagLayout是一个灵活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干个参数,解释如下: gridx/gridy:组件的横纵坐标 gridwidth ...

  9. java 网格包布局管理器_java :网格包布局管理器GridBagConstraints的应用

    代码如下:···(略)publicclassApp12_13extendsJFrame{staticApp12_13frm=newApp12_13();staticJLabeljlab=newJLab ...

最新文章

  1. json对象(json-lib)转换成list-map集合
  2. Halcon初学者知识 【11】自定义算子和应用实例
  3. Linux下rz命令和sz命令使用方法
  4. python requests_小白学 Python 爬虫(18):Requests 进阶操作
  5. P2371 [国家集训队]墨墨的等式 同余最短路
  6. LeetCode 413. 等差数列划分(DP)
  7. c++ cdi+示例_C ++“或”关键字示例
  8. Django框架架构总览
  9. 数据库理论:计算机数据库技术在信息管理中的应用分析
  10. C语言——PTA 统计素数并求和
  11. 底层码农的Stanford梦 --- 从SCPD开始 [转]
  12. 计算机编程学英语词汇,计算机编程常用英语词汇
  13. AST反混淆实战-高级难度
  14. WINDOWS 系统自定义编程 键盘
  15. 美式期权定价python_蒙特卡洛模拟和美式期权定价
  16. 信息系统项目管理师第一章复习内容(持续更新中……)
  17. MacOS配置C++开发环境
  18. Attiny416的复位系统
  19. c语言程序代码中的间隔,printf()函数输出后 默认的间隔是多少
  20. 使用簇绒图形设计原理提高您的可视化技能

热门文章

  1. 数据结构线性结构和非线性结构
  2. 查看 Python 已安装模块的方法
  3. 把字节数组转化成字符串的方法 String (byte[] bytes);String(byte[] bytes, int offset, int length);字符串数组转化成字符串
  4. 当您在malloc之后不释放时,真正发生了什么?
  5. 如何给结构体中的字符数组赋值。
  6. 服务式办公室出租,中小型企业的选择
  7. python制作热图folium_Python Folium包可以在热图上绘制标记吗?
  8. android从网络播放音乐,Android实现多媒体之播放音乐
  9. 小蓝在一张无限大的特殊画布上作画。 这张画布可以看成一个方格图,每个格子可以用一个二维的整数坐标表示。 小蓝在画布上首先点了一下几个点:(0, 0), (2020, 11), (
  10. android note分析,三星Note9七大亮点解析