Java中GridBagLayout布局管理器的用法

来源:互联网 发布:淘宝粉丝福利购卖家 编辑:程序博客网 时间:2024/04/28 01:18

1.要明确一点概念:每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为显示区域。
 网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。

2.要使用GidBagLayout要先定义一个GridBagConstraints对象。
 java API说明如下:“每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。”
 例如,如下几行代码就可以添加其它组件:
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        JFrame   f=new JFrame();
        f.setLayout(gridbag);
        Button button = new Button(name);
        gridbag.setConstraints(button, c);
        f.add(jButton);

3.为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。
 即须设置GridBagConstraints 对象的属性。我认为只要能掌握以下四种参数就能很好的使用GidBagLayout:
(1)GridBagConstraints.gridwidthGridBagConstraints.gridheight
   指定组件的显示区域行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。如下向窗口中添加一个占两个单元格(两行一列)的按钮的例子:
        JFrame   f=new JFrame();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        f.setLayout(gridbag);
        c.gridheight=2;
        c.gridwidth=1;
        JButton jButton = new JButton("按钮1");
        gridbag.setConstraints(button, c);
        f.add(jButton);
(2)GridBagConstraints.fill
   当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。
   可能的值为 GridBagConstraints.NONE(默认值)、
             GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、              

             GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和                  

           GridBagConstraints.BOTH(使组件完全填满其显示区域)。
   使用情景举例:在一个很大的窗口(如300*300)中添加一个按钮(原始大小40*30)。

(3)GridBagConstraints.anchor
   当组件小于其显示区域时,用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的ComponentOrientation 属性,而绝对值则不然。个人觉得只使用绝对值就可以。有效值有:
   绝对值
   GridBagConstraints.NORTH
   GridBagConstraints.SOUTH
   GridBagConstraints.WEST
   GridBagConstraints.EAST
   GridBagConstraints.NORTHWEST
   GridBagConstraints.NORTHEAST
   GridBagConstraints.SOUTHWEST
   GridBagConstraints.SOUTHEAST
   GridBagConstraints.CENTER (the default)

(4)GridBagConstraints.weightx、GridBagConstraints.weighty   (************最重要的属性)
 用于确定分布空间的方式,这对于指定调整行为至关重要。例如:在一个很大的窗口(如300*300)中添加两个按钮(也可以是面板)(原始大小 40*30),默认的,你会发现两个按钮分别处于上下两个等大小的区域中,且只占用了一小部分,没有被按钮占用的区域就被称为额外区域。该额外区域会随着参数weightx、weighty而被分配。


  完整的示例代码如下:

import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Example{

    public Example() {
    }

    public static void main(String args[]) {
       JFrame f = new JFrame("GridBag Layout Example");

       GridBagLayout gridbag = new GridBagLayout();
       GridBagConstraints c = new GridBagConstraints();
       f.setLayout(gridbag);
//添加按钮1
       c.fill = GridBagConstraints.BOTH;
       c.gridheight=2;
       c.gridwidth=1;
       c.weightx=0.0;//默认值为0.0
       c.weighty=0.0;//默认值为0.0
       c.anchor=GridBagConstraints.SOUTHWEST;
       JButton jButton1 = new JButton("按钮1");
       gridbag.setConstraints(jButton1, c);
       f.add(jButton1);
//添加按钮2      
       c.fill = GridBagConstraints.NONE;
       c.gridwidth=GridBagConstraints.REMAINDER;
       c.gridheight=1;
       c.weightx=1.0;//默认值为0.0
       c.weighty=0.8;
       JButton jButton2 = new JButton("按钮2");
       gridbag.setConstraints(jButton2, c);
       f.add(jButton2);
//添加按钮3
       c.fill = GridBagConstraints.BOTH;
       c.gridwidth=1;
       c.gridheight=1;
       c.weighty=0.2;
       JButton jButton3 = new JButton("按钮3");
       gridbag.setConstraints(jButton3, c);
       f.add(jButton3);

       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setSize(500,500);
       f.setVisible(true);
    }
}

   在上述代码中添加按钮2时c.weighty=0.8,而在添加按钮3时c.weighty=0.2,这就会导致按钮2所占区域的高大约是按钮3所占区域的高的0.8/0.2=4倍。

原创粉丝点击