- Swing 教程
- Swing - 首页
- Swing - 概述
- Swing - 环境
- Swing - 控件
- Swing - 事件处理
- Swing - 事件类
- Swing - 事件监听器
- Swing - 事件适配器
- Swing - 布局
- Swing - 菜单
- Swing - 容器
- Swing 有用资源
- Swing - 快速指南
- Swing - 有用资源
- Swing - 讨论
Swing - GridBagLayout 类
介绍
GridBagLayout 类以水平和垂直方式排列组件。
类声明
以下是java.awt.GridBagLayout类的声明:
public class GridBagLayout
extends Object
implements LayoutManager2, Serializable
字段
以下是java.awt.GridBagLayout类的字段:
static int DEFAULT_SIZE - 指示应为特定范围值使用组件或间隙的大小。
static int PREFERRED_SIZE - 指示应为特定范围值使用组件或间隙的首选大小。
类构造函数
| 序号 | 构造函数和描述 |
|---|---|
| 1 |
GridBagLayout() 创建一个网格包布局管理器。 |
类方法
| 序号 | 方法和描述 |
|---|---|
| 1 |
void addLayoutComponent(Component comp, Object constraints) 使用指定的约束对象将指定的组件添加到布局。 |
| 2 |
void addLayoutComponent(String name, Component comp) 将指定的组件及其名称添加到布局。 |
| 3 |
protected void adjustForGravity(GridBagConstraints constraints, Rectangle r) 根据约束几何形状和填充调整x、y宽度和高度字段到正确的值。 |
| 4 |
protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r) 此方法已过时,仅出于向后兼容性而提供;新代码应调用adjustForGravity。 |
| 5 |
protected void arrangeGrid(Container parent) 布置网格。 |
| 6 |
protected void ArrangeGrid(Container parent) 此方法已过时,仅出于向后兼容性而提供;新代码应调用arrangeGrid。 |
| 7 |
GridBagConstraints getConstraints(Component comp) 获取指定组件的约束。 |
| 8 |
float getLayoutAlignmentX(Container parent) 返回沿x轴的对齐方式。 |
| 9 |
float getLayoutAlignmentY(Container parent) 返回沿y轴的对齐方式。 |
| 10 |
int[][] getLayoutDimensions() 确定布局网格的列宽和行高。 |
| 11 |
protected java.awt.GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) 为当前管理的子项填充GridBagLayoutInfo实例。 |
| 12 |
protected java.awt.GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) 此方法已过时,仅出于向后兼容性而提供;新代码应调用getLayoutInfo。 |
| 13 |
Point getLayoutOrigin() 确定布局区域的原点,位于目标容器的图形坐标空间中。 |
| 14 |
double[][] getLayoutWeights() 确定布局网格的列和行的权重。 |
| 15 |
protected Dimension getMinSize(Container parent, java.awt.GridBagLayoutInfo info) 根据getLayoutInfo()中的信息计算主体的最小大小。 |
| 16 |
protected Dimension GetMinSize(Container parent, java.awt.GridBagLayoutInfo info) 此方法已过时,仅出于向后兼容性而提供;新代码应调用getMinSize。 |
| 17 |
void invalidateLayout(Container target) 使布局无效,指示如果布局管理器已缓存信息,则应将其丢弃。 |
| 18 |
void layoutContainer(Container parent) 使用此网格包布局布置指定的容器。 |
| 19 |
Point location(int x, int y) 确定布局网格中哪个单元格包含(x, y)指定的点。 |
| 20 |
protected GridBagConstraints lookupConstraints(Component comp) 检索指定组件的约束。 |
| 21 |
Dimension maximumLayoutSize(Container target) 返回给定指定目标容器中组件的此布局的最大尺寸。 |
| 22 |
Dimension minimumLayoutSize(Container parent) 使用此网格包布局确定父容器的最小大小。 |
| 23 |
Dimension preferredLayoutSize(Container parent) 使用此网格包布局确定父容器的首选大小。 |
| 24 |
void removeLayoutComponent(Component comp) 从此布局中删除指定的组件。 |
| 25 |
void setConstraints(Component comp, GridBagConstraints constraints) 设置此布局中指定组件的约束。 |
| 26 |
String toString() 返回此网格包布局值的字符串表示形式。 |
继承的方法
此类继承自以下类的方法:
- java.lang.Object
GridBagLayout 示例
使用您选择的任何编辑器创建以下 Java 程序,例如在D:/ > SWING > com > tutorialspoint > gui >中。
SwingLayoutDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showGridBagLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showGridBagLayoutDemo(){
headerLabel.setText("Layout in action: GridBagLayout");
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button 1"),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new JButton("Button 2"),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JButton("Button 3"),gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(new JButton("Button 4"),gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new JButton("Button 5"),gbc);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
使用命令提示符编译程序。转到D:/ > SWING并键入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingLayoutDemo.java
如果未出现错误,则表示编译成功。使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingLayoutDemo
验证以下输出。