• JavaAwtSwing布局 GridBagLayout和GridBagConstraints测试-220610


    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
        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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    两者都不配置 , 不会换行

    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);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    效果

    在这里插入图片描述

    只配置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);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里插入图片描述

    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);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里插入图片描述

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述
    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    如果多次在一个位置添加(相同的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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    如果移除第一个元素 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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    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);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

  • 相关阅读:
    【k8s】1、基础概念和架构及组件
    IOCP模型C++入门级服务端搭建
    Java策略模式之总有你想不到的知识
    2022京东双十一全品类销售额变化情况一览:50%增长,50%下滑
    jar包启动命令
    vue和react的区别
    基于MPPT的太阳能光伏电池simulink性能仿真,对比扰动观察法,增量电导法,恒定电压法
    react笔记之第一个react项目
    张家界四日研学旅行
    Ubuntu 24.04 抢先体验换国内源 清华源 阿里源 中科大源 163源
  • 原文地址:https://blog.csdn.net/kfepiza/article/details/125215269