求助:GridBagLayout详细用法的例子,谢谢!

解决方案 »

  1.   

    JDK 原文为什么不看呢?
    java.awt 
    类 GridBagLayout
    java.lang.Object
      java.awt.GridBagLayout
    所有已实现的接口: 
    LayoutManager, LayoutManager2, Serializable --------------------------------------------------------------------------------public class GridBagLayoutextends Objectimplements LayoutManager2, SerializableGridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同即可将组件垂直和水平对齐。每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为显示区域。 每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。除了 Constraints 对象之外,GridBagLayout 还考虑每个组件的最小和首选大小,以确定组件的大小。 网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。 为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。可以通过设置一个或多个实例变量来自定义 GridBagConstraints 对象: 
    GridBagConstraints.gridx、GridBagConstraints.gridy 
    指定包含组件显示区域的前导角的单元,在此显示区域中,位于网格原点的单元地址是 gridx = 0、gridy = 0。对于水平的从左到右的布局,组件的前导角是其左上角。对于水平的从右到左的布局,组件的前导角是其右上角。使用 GridBagConstraints.RELATIVE(默认值)指定将组件置于添加此组件前刚刚添加到容器组件的后面(沿 gridx 的 X 轴或 gridy 的 Y 轴)。 
    GridBagConstraints.gridwidth、GridBagConstraints.gridheight 
    指定组件的显示区域中行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。使用 GridBagConstraints.REMAINDER 指定组件的显示区域为从 gridx 到该行(针对 gridwidth)中的最后一个单元,或者从 gridy 到该列(针对 gridheight)中的最后一个单元。 使用 GridBagConstraints.RELATIVE 指定组件的显示区域为从 gridx 到其所在行(针对 gridwidth)的倒数第二个单元,或者从 gridy 到其所在列(针对 gridheight)的倒数第二个单元。 
    GridBagConstraints.fill 
    当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。可能的值为 GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和 GridBagConstraints.BOTH(使组件完全填满其显示区域)。 
    GridBagConstraints.ipadx、GridBagConstraints.ipady 
    指定布局中组件的内部填充,对组件最小大小的添加量。组件的宽度至少为其最小宽度加上 ipadx 像素。类似地,组件的高度至少为其最小高度加上 ipady 像素。 
    GridBagConstraints.insets 
    指定组件的外部填充,组件与其显示区域边缘之间间距的最小量。 
    GridBagConstraints.anchor 
    当组件小于其显示区域时,用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的 ComponentOrientation 属性,而绝对值则不然。有效值有: 绝对值
     相对值
     
    GridBagConstraints.NORTH 
    GridBagConstraints.SOUTH 
    GridBagConstraints.WEST 
    GridBagConstraints.EAST 
    GridBagConstraints.NORTHWEST 
    GridBagConstraints.NORTHEAST 
    GridBagConstraints.SOUTHWEST 
    GridBagConstraints.SOUTHEAST 
    GridBagConstraints.CENTER (the default) 
     GridBagConstraints.PAGE_START 
    GridBagConstraints.PAGE_END 
    GridBagConstraints.LINE_START 
    GridBagConstraints.LINE_END 
    GridBagConstraints.FIRST_LINE_START 
    GridBagConstraints.FIRST_LINE_END 
    GridBagConstraints.LAST_LINE_START 
    GridBagConstraints.LAST_LINE_END 
     
    GridBagConstraints.weightx、GridBagConstraints.weighty 
    用于确定分布空间的方式,这对于指定调整行为至关重要。除非在行 (weightx) 和列 (weighty) 中至少指定一个组件的权重,否则所有组件都会聚集在其容器的中央。这是因为,当权重为零(默认值)时,GridBagLayout 对象会将所有额外空间置于其单元网格和容器边缘之间。 
    下图显示了由网格包布局管理的十个组件(均为按钮)。图 1 显示水平方向从左到右容器的布局,图 2 显示水平方向从右到左容器的布局。 
        
    图 1:水平方向,从左到右 图 2:水平方向,从右到左 这十个组件的每一个都将与其相关的 GridBagConstraints 对象的 fill 字段设置为 GridBagConstraints.BOTH。此外,这些组件还有以下非默认值约束 (Constraints): 
    Button1、Button2、Button3: weightx = 1.0 
    Button4:weightx = 1.0、gridwidth = GridBagConstraints.REMAINDER 
    Button5:gridwidth = GridBagConstraints.REMAINDER 
    Button6:gridwidth = GridBagConstraints.RELATIVE 
    Button7:gridwidth = GridBagConstraints.REMAINDER 
    Button8:gridheight = 2、weighty = 1.0 
    Button9、Button 10:gridwidth = GridBagConstraints.REMAINDER 
    下面是实现上述示例的代码: -------------------------------------------------------------------------------- import java.awt.*;
     import java.util.*;
     import java.applet.Applet; public class GridBagEx1 extends Applet {     protected void makebutton(String name,
                                   GridBagLayout gridbag,
                                   GridBagConstraints c) {
             Button button = new Button(name);
             gridbag.setConstraints(button, c);
             add(button);
         }     public void init() {
             GridBagLayout gridbag = new GridBagLayout();
             GridBagConstraints c = new GridBagConstraints();         setFont(new Font("SansSerif", Font.PLAIN, 14));
             setLayout(gridbag);         c.fill = GridBagConstraints.BOTH;
             c.weightx = 1.0;
             makebutton("Button1", gridbag, c);
             makebutton("Button2", gridbag, c);
             makebutton("Button3", gridbag, c);           c.gridwidth = GridBagConstraints.REMAINDER; //end row
             makebutton("Button4", gridbag, c);         c.weightx = 0.0;                  //reset to the default
             makebutton("Button5", gridbag, c); //another row           c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
             makebutton("Button6", gridbag, c);           c.gridwidth = GridBagConstraints.REMAINDER; //end row
             makebutton("Button7", gridbag, c);           c.gridwidth = 1;                //reset to the default
               c.gridheight = 2;
             c.weighty = 1.0;
             makebutton("Button8", gridbag, c);         c.weighty = 0.0;                  //reset to the default
               c.gridwidth = GridBagConstraints.REMAINDER; //end row
               c.gridheight = 1;               //reset to the default
             makebutton("Button9", gridbag, c);
             makebutton("Button10", gridbag, c);         setSize(300, 100);
         }     public static void main(String args[]) {
               Frame f = new Frame("GridBag Layout Example");
               GridBagEx1 ex1 = new GridBagEx1();           ex1.init();           f.add("Center", ex1);
               f.pack();
               f.setSize(f.getPreferredSize());
               f.show();
         }
     }
      

  2.   

    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);
         }
    }