了解GridBagLayout约束

我是一个相对较新的摇摆,我试图弄清楚它是如何工作的。 告诉我,如果我错了。

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

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

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

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

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

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

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

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

如何使按钮(例如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个单元格。

gridwidth,gridheight

根据单元格指定列数(对于网格宽度)或行(对于网格高度),组件的显示区域可以由添加了这些约束的组件占用。 默认值为1

那么,如果是( R x C )的网格; 连续有C个单元格。 如果要让组件占用该特定行的所有单元格,只需将GridBigConstraint.gridWidth设置为C

使用GridBagConstraints.REMAINDER指定组件是其行(对于gridwidth)或列(对于gridheight)中的最后一个组件。

本教程中已有一个很好的书面示例: 如何使用GridBagLayout

你的意思是这样的……

在此处输入图像描述

 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)。 这将永远扩展正确。

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

更新

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

实现类似……

在此处输入图像描述

最简单的选择是添加一个“填充”组件,例如……

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

我说我想要或多或少像这样的东西: http : //postimg.org/image/7a5vi8t21/

并且由于所有的澄清,我能够或多或少地做到这一点。 我使用了一个额外的透明按钮。 这是输出图像: http : //postimg.org/image/6yw3c8b37/

以下是代码

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

我想我终于明白了网格宽度和网格高度是如何工作的。 他们从左到右,从上到下工作。 它们也在空行或列中支持组件的缺失中崩溃。

但是,仍有一些问题我不明白。 如果我使用透明标签(lb1)而不是透明按钮b5,则列的尺寸会发生变化。 另外,为什么b4的宽度不是其他按钮的两倍,尽管重量是两倍?