Understanding GridBagLayout constraints

喜欢而已 提交于 2019-12-03 21:23:01

gridwidth, gridheight:

Specify the number of columns (for gridwidth) or rows (for gridheight) in terms of cells the component's display area can be occupied by the component added with these constraint defined. The default value is 1.

So, If for a grid of (R x C); there are C number of cells in a row. If you want to have a component to occupy all the cell of that specific row, just set the GridBigConstraint.gridWidth to C.

Use GridBagConstraints.REMAINDER to specify that the component be the last one in its row (for gridwidth) or column (for gridheight).

There is already a nice written example in the tutorial: How to use GridBagLayout

You mean something like this...

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagTest {

    public static void main(String[] args) {
        JFrame f1 = new JFrame("GridBag Layout Test");

        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setResizable(true);
        f1.setLocation(200, 100);
        f1.setSize(800, 800);

        JPanel p1 = new JPanel();
        p1.setBackground(Color.black);
        f1.add(p1);

        JButton b1 = new JButton("Button 1");
        b1.setBackground(Color.white);

        JButton b2 = new JButton("Button 2");
        b2.setBackground(Color.white);

        JButton b3 = new JButton("Button 3");
        b3.setBackground(Color.white);

        JButton b4 = new JButton("Button 4");
        b4.setBackground(Color.white);

        GridBagLayout gm1 = new GridBagLayout();
        p1.setLayout(gm1);
        GridBagConstraints cns = new GridBagConstraints();

        cns.gridx = 5;
        cns.gridy = 5;
        cns.weightx = 0.1;
        cns.weighty = 0.1;
        cns.fill = GridBagConstraints.BOTH;

        p1.add(b1, cns);

        cns.gridx = 3;
        cns.gridy = 3;
//        cns.gridheight = GridBagConstraints.REMAINDER;
        cns.gridwidth = 3;
        p1.add(b2, cns);

        cns.gridheight = 1;
        cns.gridwidth = 1;
        cns.gridx = 4;
        cns.gridy = 9;
        p1.add(b3, cns);

        cns.gridx = 6;
        cns.gridy = 3;
        cns.gridheight = 7;
        cns.weightx = 0.2;
        cns.weighty = 0.2;
//        cns.gridheight = 4;
        p1.add(b4, cns);

        f1.setVisible(true);

    }
}

gridwidth basically tells GridBagLayout how many columns a component should expand across (the default being 1). This will always expand right.

gridheight does the same, expect that it will always expand down...

Updated

GridBagLayout is very powerful, but even then, sometimes, it has limitations.

To achieve something like...

The simplest choice is to addd a "filler" component, for example...

cns.gridx = 3;
cns.gridy = 9;
cns.gridwidth = 1;
JPanel pnl = new JPanel();
pnl.setOpaque(false);
p1.add(pnl, cns);

I said I want something like more or less like this: http://postimg.org/image/7a5vi8t21/

and thanks to all the clarifications I am able to do more or less that. I used an extra transparent button. here is the ouput image: http://postimg.org/image/6yw3c8b37/

and below is the code

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

public class GridBagTest2 
{

    public static void main(String[] args) {
        JFrame f1 = new JFrame("GridBag Layout Test");

        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setResizable(true);
        f1.setLocation(200, 100);
        f1.setSize(800, 800);

        JPanel p1 = new JPanel();
        p1.setBackground(Color.black);
        f1.add(p1);

        JButton b1 = new JButton("Button 1");
        b1.setBackground(Color.white);

        JButton b2 = new JButton("Button 2");
        b2.setBackground(Color.white);

        JButton b3 = new JButton("Button 3");
        b3.setBackground(Color.white);

        JButton b4 = new JButton("Button 4");
        b4.setBackground(Color.white);

        JButton b5 = new JButton("        ");
        b5.setBackground(Color.white);
        b5.setBorderPainted(false);
        b5.setOpaque(false);

        JLabel lb1 = new JLabel("");
        //lb1.setOpaque(true);

        GridBagLayout gm1 = new GridBagLayout();
        p1.setLayout(gm1);
        GridBagConstraints cns = new GridBagConstraints();

        cns.gridx = 5;
        cns.gridy = 5;
        cns.weightx = 0.1;
        cns.weighty = 0.1;
        cns.fill = GridBagConstraints.BOTH;

        p1.add(b1, cns);

        cns.gridx = 3;
        cns.gridy = 3;

        cns.gridwidth = 3;
        p1.add(b2, cns);

        cns.gridheight = 1;
        cns.gridwidth = 1;
        cns.gridx = 4;
        cns.gridy = 9;
        p1.add(b3, cns);

        cns.gridx = 7;
        cns.gridy = 3;
        cns.gridheight = 8;
        cns.weightx = 0.2;
        cns.weighty = 0.2;

        p1.add(b4, cns);


        cns.gridx = 3;
        cns.gridy = 10;
        cns.gridheight=1;
        cns.gridwidth = 1;
        cns.weightx = 0.1;
        cns.weighty = 0.1;
        p1.add(b5, cns);


        f1.setVisible(true);

    }
}

I think I am finally understanding how gridwidth and gridheight works. They work from left to right and top to down. They also collapse in the absense of supporting components in empty rows or columns.

however, there are still some questions I don't understand. If I use a transparent label (lb1) instead of the transparent button b5, the dimension of the column changes. Also, why is b4 not twice as wide as the other buttons despite having twice the weight?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!