Java GridBagLayout
精华
小牛编辑
150浏览
2023-03-14
1 Java GridBagLayout的介绍
Java GridBagLayout类用于垂直,水平或沿其基线对齐组件。
组件的大小可能不同。每个GridBagLayout对象都维护一个动态的矩形单元格网格。每个组件占据一个或多个单元格(称为其显示区域)。每个组件都关联一个GridBagConstraints实例。借助约束对象,我们在网格上排列了组件的显示区域。GridBagLayout管理每个组件的最小和首选大小,以确定组件的大小。
2 Java GridBagLayout的字段
字段 | 描述 |
---|---|
double[] columnWeights | 用于将替代值保留到列权重。 |
int[] columnWidths | 用于将替代保留到列的最小宽度。 |
protected Hashtable<Component,GridBagConstraints> comptable | 用于维护组件与其网格袋约束之间的关联。 |
protected GridBagConstraints defaultConstraints | 用于保存包含默认值的gridbag约束实例。 |
protected GridBagLayoutInfo layoutInfo | 用于保存网格袋的布局信息。 |
protected static int MAXGRIDSIZE | 不再只是为了向后兼容而使用 |
protected static int MINSIZE | 可以通过网格袋布局布置的最小网格。 |
protected static int PREFERREDSIZE | 优选的网格尺寸可以通过网格袋布局来布局。 |
int[] rowHeights | 用于将替代控件保持在行的最小高度。 |
double[] rowWeights | 用于保留对行权重的替代。 |
3 Java GridBagLayout的方法
方法 | 描述 |
---|---|
void addLayoutComponent(Component comp, Object constraints) | 使用指定的约束对象将指定的组件添加到布局中。 |
void addLayoutComponent(String name, Component comp) | 这没有效果,因为此布局管理器不使用每个组件的字符串。 |
protected void adjustForGravity(GridBagConstraints constraints, Rectangle r) | 将根据约束几何图形和填充将x,y,宽度和高度字段调整为正确的值。 |
protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r) | 此方法仅用于向后兼容 |
protected void arrangeGrid(Container parent) | 布置网格。 |
protected void ArrangeGrid(Container parent) | 此方法已过时,并提供向后兼容 |
GridBagConstraints getConstraints(Component comp) | 用于获取指定组件的约束。 |
float getLayoutAlignmentX(Container parent) | 返回沿x轴的对齐方式。 |
float getLayoutAlignmentY(Container parent) | 返回沿y轴的对齐方式。 |
int[][] getLayoutDimensions() | 确定布局网格的列宽和行高。 |
protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) | 此方法已过时,为了向后兼容而提供。 |
protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) | 此方法已过时,为了向后兼容而提供。 |
Point getLayoutOrigin() | 在目标容器的图形坐标空间中确定布局区域的原点。 |
double[][] getLayoutWeights() | 确定布局网格的列和行的权重。 |
protected Dimension getMinSize(Container parent, GridBagLayoutInfo info) | 根据getLayoutInfo中的信息计算出母版的最小大小。 |
protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) | 该方法已过时,仅用于向后兼容 |
4 Java GridBagLayout的案例1
package cn.xnip;
/**
* 小牛知识库网: https://www.xnip.cn
*/
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends JFrame{
public static void main(String[] args) {
GridBagLayoutExample a = new GridBagLayoutExample();
}
public GridBagLayoutExample() {
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBagLayout案例-小牛知识库网");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
输出结果为:
5 Java GridBagLayout的案例2
package cn.xnip;
/**
* 小牛知识库网: https://www.xnip.cn
*/
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayout案例-小牛知识库网");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
输出结果为: