Swing-GridBagLayout用法-入门

时间:2022-01-05 16:20:15

注:本文内容转自:Java Layout总结-GridBagLayout。内容根据笔者理解稍有整理。

GridBagLayout布局管理器:
这就是最复杂的一个布局管理器了,网格包布局.在此布局中,组件大小不必相同.
GridBagLayout gb=new GridBagLayout(); 
ContainerName.setLayout(gb);

以上代码是让容器获得一个GridBagLayout.要使用网格包布局,还必须有其一个辅助类,GridBagContraints.它包含GridBagLayout类用来定位及调整组件大小所需要的全部信息.使用步骤如下:

1).创建网格包布局的一个实例,并将其定义为当前容器的布局管理器.

2).创建GridBagContraints的一个实例

3).为组件设置约束.

4).通过方法统治布局管理器有关组件及其约束等信息

5).将组件添加到容器.

6).对各个将被显示的组件重复以上步骤.

GridBagContraints类的成员变量列表如下:

1、gridx—组件的横向坐标;
2、girdy—组件的纵向坐标;

gridx=0,gridy=0时放在0行0列,GridBagConstraints.RELATIVE为默认值,表明当前组件紧跟在上一个组件之后。如果在行上不指定该值(同时也不指定gridx和gridy参数),那么无论添加多少个组件都是在同一行上

3、gridwidth——组件的横向宽度,也就是指组件占用的列数,这与HTML的colspan类似;
4、gridheight—组件的纵向长度,也就是指组件占用的行数,这与HTML的rowspan类似;

设置组件横向纵向跨越多少个网格,他们的默认值都是1,如果该组件是横向或纵向的最后一个还可以将此值设为GridBagConstraints.REMAINDER,若为倒数第二个组件则可以设值为GridBagConstraints.RELATIVE。

gridwidth能否能够保留住该宽度取决于其正上/下方相应位置是否有足够的组件来占位,如果无,位置将会被压缩。比如,设置gridwidth=3,但只添加了一个JButton,这时如果其上下方相应位置没有其他组件或只有1个组件,那么它只占有1个网格大小。如果它上/下方相应位置有3个组件,那它就可以占3个网格大小了。如果它上/下方相应位置只有2个组件,那它就占2个网格大小。

gridheight能否保留住该高度取决于其左右两边是否有足够的组件来占位。也就是说它的最大高度不大于左右两边最大的高度。比如,设置gridheight=3,如果左右两边组件只有1行,则它仅仅只有1行的高度,有2行则占2行的高度。

5、weightx—指行的权重,告诉布局管理器如何分配额外的水平空间;
6、weighty—指列的权重,告诉布局管理器如何分配额外的垂直空间;

用来设置窗口变大时,各组件跟着变大的比例。当数字越大,表示组件能得到更多的空间,默认值皆为0。比如组件A的weightx=0.5,组件B的weightx=1,那么窗口X轴变大时剩余的空间就会以1:2的比例分配给组件A和B;

7、anchor—告诉布局管理器组件在表格空间中的位置,当组件小于其显示区域时使用此字段;

有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。

8、fill—如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充;

9、insets—指组件与表格空间四周边缘的空白区域的大小,内边距,算入组件自身大小中。它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。

10、ipadx— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值;
11、ipady— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值。

外边距,最终组件占用的大小是组件自身大小加上外边距。

下面是测试用例:

GridBagLayoutTest.java:
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/*
* GridBagLayoutTest.java,source code from java核心技术 卷1 基础知识,P379
*/ public class GridBagLayoutTest { public static void main(String[] args) {
// TODO Auto-generated method stub FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} class FontFrame extends JFrame {
int DEUAULT_WIDTH = 300;
int DEUAULT_HEIGHT = 200;
private JComboBox face;
private JComboBox size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample; public FontFrame() {
setTitle("GridBagLayoutTest");
setSize(DEUAULT_WIDTH, DEUAULT_HEIGHT);
GridBagLayout layout = new GridBagLayout();
setLayout(layout); ActionListener listener = new FontAction();
JLabel faceLabel = new JLabel("face:");
face = new JComboBox(new String[] { "serif", "sansSerif", "Monospaced",
"Dialog", "DialogInput", });
face.addActionListener(listener); JLabel sizeLabel = new JLabel("Size:");
size = new JComboBox(new String[] { "8", "10", "12", "15", "18", "24",
"36", "48" });
size.addActionListener(listener); bold = new JCheckBox("Bold");
bold.addActionListener(listener); italic = new JCheckBox("Italic");
italic.addActionListener(listener); sample = new JTextArea();
sample.setText("The quick brown fox jump over the lazy dog.");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder()); add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(italic,
new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
} private class FontAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String fontFace = face.getSelectedItem().toString();
int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
+ (italic.isSelected() ? Font.ITALIC : 0);
int fontSize = Integer.parseInt(size.getSelectedItem().toString());
Font font = new Font(fontFace, fontStyle, fontSize);
sample.setFont(font);
sample.repaint();
}
}
}
GBC.java:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets; /*
* GBC.java,source code from java核心技术 卷1 基础知识,P381
*/ public class GBC extends GridBagConstraints{
/*
* constructs a GBC with a given gridx and gridy position and all other grid
* bag constraint values set to the default
* @param gridx the gridx position
* @param gridy the gridy position
*/
public GBC(int gridx, int gridy){
this.gridx = gridx;
this.gridy = gridy;
} public GBC(int gridx, int gridy, int gridWidth, int gridHeight){
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridWidth;
this.gridheight = gridHeight;
} /*
* sets the anchor
* @param anchor the anchor style
* @return this object for further modification
*/ public GBC setAnchor(int anchor){
this.anchor = anchor;
return this;
} /*
* sets the fill direction
* @param fill the fill direction
* @return this object for further modification
*/ public GBC setFill(int fill){
this.fill = fill;
return this;
} /*
* sets the cell weights
* @param weightx the cell weight in x direction
* @param weighty the cell weight in y direction
* @return this object for further modification
*/ public GBC setWeight(int weightx, int weighty){
this.weightx = weightx;
this.weighty = weighty;
return this;
} /*
* sets the insets of this cell
* @param insets distance ths spacing to use in all directions
* @return this object for further modification
*/ public GBC setInsets(int distance){
this.insets = new Insets(distance, distance, distance, distance);
return this;
} /*
* sets the insets of this cell
* @param top distance ths spacing to use on top
* @param bottom distance ths spacing to use on bottom
* @param left distance ths spacing to use to the left
* @param right distance ths spacing to use to the right
* @return this object for further modification
*/ public GBC setInsets(int top, int left,int bottom,int right){
this.insets = new Insets(top, left, bottom, right);
return this;
} /*
* sets the Ipad of this cell
* @param Ipad distance ths spacing to use in all directions
* @return this object for further modification
*/ public GBC setIpad(int ipadx, int ipady){
this.ipadx = ipadx;
this.ipadx = ipadx;
return this;
}
}

Swing-GridBagLayout用法-入门

运行效果图