了解GridBagLayout约束 [英] Understanding GridBagLayout constraints

查看:154
本文介绍了了解GridBagLayout约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相对较新,我正在努力弄清楚它是如何工作的。告诉我,如果我错了。

I am relatively new to swing and I am trying to figure out exactly how it works. Tell me if I am wrong.

我主要想出了放置组件所需的gridx和gridy。您至少需要n个值(gridx,gridy)才能创建nxn网格。例如(5,5),(3,3),(4,9),(3,10)将创建一个3x4网格空间(4行,3列),其中组件分别使用上面的(gridx,gridy)放置在单元格(3,2),(1,2),(2,3),(1,4)中。

I have mostly figured out gridx and gridy which is necessary to place a component. You need at least n values of (gridx,gridy) to create an nxn grid. For example (5,5),(3,3),(4,9),(3,10) will create a 3x4 gridspace(4 rows, 3 columns) with the components using the above (gridx,gridy) respectively placed in cells (3,2),(1,2),(2,3),(1,4).

weightx和weighty似乎有2个函数, a> 0 weightx和weighy的值将网格单元拉伸到边缘(否则它是集中的),我们也可以设置组件大小的比例 - 所以如果一个组件有gridx = 0.1而anothr有0.2,那么后者是可能是两倍宽。但是,在按比例分量时,会考虑组件的最小默认宽度和高度。

weightx and weighty seems to have 2 functions, a >0 value of weightx and weighy stretches the grid cells to the edges(otherwise it's centralised) and we can also set proportions to size of the component - so if one component has gridx=0.1 and anothr has 0.2, then the later one is likely to be twice as wide. However, while proportioning components, the minimum default width and height of a component is respected.

需要填充以将组件拉伸到单元格的边缘。如果没有填充,组件将保持在中心。

fill is required to stretch a component to the edges of the cells. Without fill, the component will remain at the center.

但是我们可以在这种情况下使用锚点来定位组件沿着单元格的西北角而不是中心位置。

but we can use anchor in such cases to position the component say along the north west corner of the cell instead of center.

insets在单元格边缘内创建一个空间墙。

insets create a wall of space inside the edges of the cell.

ipadx和ipady推动了界限包含单元格的列或行。

ipadx and ipady pushes the boundaries of the column or row containing the cell.

但是我无法弄清楚网格宽度和网格高度是如何工作的。

But I am not able to figure out very well how gridwidth and gridheight works.

考虑下面的示例。这4个按钮放在单元格b1(3,2),b2(1,1),b3(2,3)中),b4(4,4)
的4x4网格空间。

Consider this example below.Here 4 buttons are placed in cells b1(3,2),b2(1,1),b3(2,3),b4(4,4) of 4x4 gridspace.

如何使按钮(例如b2或b4)占据整行它占据的单元格或列?

import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
public class Swing29a
{
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;
p1.add(b2,cns);


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

cns.gridx=7;//3;
cns.gridy=10;
cns.gridheight=3;
cns.weightx=0.2;
cns.weighty=0.2;
//cns.weightx=10.0;
//cns.weighty=9.0;    
//cns.ipadx=50;
//cns.ipady=50;
p1.add(b4,cns);

f1.setVisible(true);

}
}

编辑:
这是图片链接
http:/ /postimg.org/image/wae2x4w4z/

我希望任何按钮能够填充整行或列,或者至少需要2连续的单元格。

I want any of the buttons to be able to fill the whole row or column, or take at least 2 cells in a row.

推荐答案

你的意思是这样......

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 基本上告诉 GridBagLayout 组件应扩展多少列(默认为1)。这将始终正确扩展。

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

gridheight 做同样的事情,期望它会一直向下扩展...

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

已更新

GridBagLayout 非常强大,但即便如此,有时它也有局限性。

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);

这篇关于了解GridBagLayout约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆