443.md 5.0 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 如何使用 GridLayout

> 原文: [https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html](https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html)

* * *

**Note:** This lesson covers writing layout code by hand, which can be challenging. If you are not interested in learning all the details of layout management, you might prefer to use the `GroupLayout` layout manager combined with a builder tool to lay out your GUI. One such builder tool is the [NetBeans IDE](../learn/index.html). Otherwise, if you want to code by hand and do not want to use `GroupLayout`, then `GridBagLayout` is recommended as the next most flexible and powerful layout manager.

* * *

如果您对使用 JavaFX 创建 GUI 感兴趣,请参阅[在 JavaFX](https://docs.oracle.com/javase/8/javafx/layout-tutorial/index.html) 中使用布局。

下图表示使用 [`GridLayout`](https://docs.oracle.com/javase/8/docs/api/java/awt/GridLayout.html) 类的应用程序的快照。

W
wizardforcel 已提交
15
![A snapshot of GridLayoutDemo](img/014b93e45655af3e7a1f69b4c75c538e.jpg)
W
init  
wizardforcel 已提交
16 17 18 19 20 21 22 23 24 25 26

单击“启动”按钮以使用 [Java™Web Start](http://www.oracle.com/technetwork/java/javase/javawebstart/index.html)[下载 JDK 7 或更高版本](http://www.oracle.com/technetwork/java/javase/downloads/index.html))运行 GridLayoutDemo。或者,要自己编译并运行示例,请参考[示例索引](../examples/layout/index.html#GridLayoutDemo)

[![Launches the GridLayoutDemo application](img/4707a69a17729d71c56b2bdbbb4cc61c.jpg)](https://docs.oracle.com/javase/tutorialJWS/samples/uiswing/GridLayoutDemoProject/GridLayoutDemo.jnlp)

该演示的完整代码位于 [`GridLayoutDemo.java`](../examples/layout/GridLayoutDemoProject/src/layout/GridLayoutDemo.java) 文件中。

`GridLayout`对象将组件放置在单元格网格中。每个组件占用其单元中的所有可用空间,并且每个单元的大小完全相同。如果`GridLayoutDemo`窗口调整大小,`GridLayout`对象会更改单元格大小,以便在给定容器可用空间的情况下,单元格尽可能大。

下面的代码片段创建`GridLayout`对象及其管理的组件。

W
wizardforcel 已提交
27
```java
W
init  
wizardforcel 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
GridLayout experimentLayout = new GridLayout(0,2);

...

        compsToExperiment.setLayout(experimentLayout);

        compsToExperiment.add(new JButton("Button 1"));
        compsToExperiment.add(new JButton("Button 2"));
        compsToExperiment.add(new JButton("Button 3"));
        compsToExperiment.add(new JButton("Long-Named Button 4"));
        compsToExperiment.add(new JButton("5"));

```

W
wizardforcel 已提交
42
`GridLayout`类的构造器根据需要创建一个具有两列和多行的实例。
W
init  
wizardforcel 已提交
43 44 45

使用组合框设置在组件周围放置多少垂直或水平填充。然后单击“应用间隙”按钮。以下代码段显示了如何使用`GridLayout`类的`setVgap``setHgap`方法处理选择:

W
wizardforcel 已提交
46
```java
W
init  
wizardforcel 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
applyButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //Get the horizontal gap value
                String horGap = (String)horGapComboBox.getSelectedItem();
                //Get the vertical gap value
                String verGap = (String)verGapComboBox.getSelectedItem();
                //Set up the horizontal gap value
                experimentLayout.setHgap(Integer.parseInt(horGap));
                //Set up the vertical gap value
                experimentLayout.setVgap(Integer.parseInt(verGap));
                //Set up the layout of the buttons
                experimentLayout.layoutContainer(compsToExperiment);
            }
        });

```

W
wizardforcel 已提交
64
下表列出了`GridLayout`类的构造器,它指定了行数和列数。
W
init  
wizardforcel 已提交
65

W
wizardforcel 已提交
66
The `GridLayout` class constructors
W
wizardforcel 已提交
67
| 构造器 | 目的 |
W
init  
wizardforcel 已提交
68
| --- | --- |
W
wizardforcel 已提交
69 70
| [`GridLayout(int _ 行*,int _cols_ )`](https://docs.oracle.com/javase/8/docs/api/java/awt/GridLayout.html#GridLayout-int-int-) | 创建具有指定行数和列数的网格布局。布局中的所有组件都具有相同的大小。 `rows``cols`中的一个(但不是两个)可以为零,这意味着可以将任意数量的对象放置在行中或列中。 |
| [`GridLayout(int _ 行*,int _cols_ ,int _hgap_ ,int _vgap_ )`](https://docs.oracle.com/javase/8/docs/api/java/awt/GridLayout.html#GridLayout-int-int-int-int-) | 创建具有指定行数和列数的网格布局。此外,水平和垂直间隙设置为指定值。水平间隙是每列之间的位置。在每行之间放置垂直间隙。 |
W
init  
wizardforcel 已提交
71

W
wizardforcel 已提交
72
`GridLayout`类有两个构造器:
W
init  
wizardforcel 已提交
73 74 75 76 77 78 79 80 81 82

##  的示例

下表列出了使用`GridLayout`类并提供相关部分链接的代码示例。

| 例 | 在哪里描述 | 笔记 |
| :-- | :-- | :-- |
| [`GridLayoutDemo`](../examples/layout/index.html#GridLayoutDemo) | 这一页 | 使用 2 列网格。 |
| [`ComboBoxDemo2`](../examples/components/index.html#ComboBoxDemo2) | [如何使用组合框](../components/combobox.html) | 使用 1x1 网格使组件尽可能大的许多示例之一。 |
| [`LabelDemo`](../examples/components/index.html#LabelDemo) | [如何使用标签](../components/label.html) | 使用 3 行网格。 |