[转载]布局管理器GridBagLayout的使用

[J] 布局管理器 GridBagLayout 的使用

 

Java布局管理器有FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout等,相比较而言,GridBagLayout的使用方式要复杂一些。但它的表现力也最丰富。之前我对GridBagLayout的使用也是一知半解,具体的用法不是十分的明了。这两天认真的看了这一块,就写了下来。

使用 GridBagLayout的主要工作就是设置组件的约束,通过GridBagConstraints的实例设置。

GridBagConstraints 主要有11个参数:gridx, gridy, gridwidth ,gridheight ,anchor ,fill ,weightx ,weighty ,insets ,ipadx ,ipady;

每个参数的意义可以参考java api上的说明。本文的最后附有anchor属性和fill属性的示例。实际使用中根据自己的需要设置特定的属性。

在Java api中给出了一个GridBagLayout的例子和实现,下面的是自己的实现方式。

[转载]布局管理器GridBagLayout的使用_第1张图片

[CODE]

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class GridBagLayoutDemo extends Applet {

GridBagLayout gbaglayout;

GridBagConstraints constraints;

public void init() {

   gbaglayout = new GridBagLayout();  

   constraints = new GridBagConstraints();

   setLayout(gbaglayout);

  

   //Button 1

   Button btn1 = new Button("Button01");

   constraints.gridx = 0;

   constraints.gridy = 0;

   constraints.gridwidth = 1;

   constraints.gridheight = 1;

   constraints.anchor = GridBagConstraints.CENTER;

   constraints.fill = GridBagConstraints.NONE;

   constraints.weightx = 0.0;

   constraints.weighty = 0.0;

   constraints.insets = new Insets(0, 0, 0, 0);

   constraints.ipadx = 0;

   constraints.ipady = 0;

   makeButton(gbaglayout, constraints, btn1);

  

   //Button 2

   Button btn2 = new Button("Button02");

   constraints.gridx = 1;  

   makeButton(gbaglayout, constraints, btn2);

  

   //Button 3

   Button btn3 = new Button("Button03");

   constraints.gridx = 2;  

   makeButton(gbaglayout, constraints, btn3);

  

   //Button 4

   Button btn4 = new Button("Button04");

   constraints.gridx = 3;  

   makeButton(gbaglayout, constraints, btn4);

  

//Button 5

   Button btn5 = new Button("Button05");

   constraints.gridx = 0;

   constraints.gridy = 1;

   constraints.gridwidth = 4;

   makeButton(gbaglayout, constraints, btn5);

  

   //Button 6

   Button btn6 = new Button("Button06");

   constraints.gridx = 0;

   constraints.gridy = 2;

   constraints.gridwidth = 3;

   makeButton(gbaglayout, constraints, btn6);

  

   //Button 7

   Button btn7 = new Button("Button07");

   constraints.gridx = 3;

   constraints.gridy = 2;

   constraints.gridwidth = 1;

   makeButton(gbaglayout, constraints, btn7);

  

   //Button 8

   Button btn8 = new Button("Button08");

   constraints.gridx = 0;

   constraints.gridy = 3;

   constraints.gridwidth = 1;

   constraints.gridheight = 2;  

   makeButton(gbaglayout, constraints, btn8);

  

   //Button 9

   Button btn9 = new Button("Button09");

   constraints.gridx = 1;

   constraints.gridy = 3;

   constraints.gridwidth = 3;

   constraints.gridheight = 1;  

   makeButton(gbaglayout, constraints, btn9);

  

   //Button 10

   Button btn10 = new Button("Button10");

   constraints.gridx = 1;

   constraints.gridy = 4;

   constraints.gridwidth = 3;

   constraints.gridheight = 1;  

   makeButton(gbaglayout, constraints, btn10);

}

private void makeButton(GridBagLayout gbaglayout, GridBagConstraints constraints, Component component) {

   gbaglayout.setConstraints(component, constraints);

   add(component);

}

public static void main(String[] args) {

   Frame frame = new Frame("GridBagLayoutDemo");

   frame.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

     System.exit(0);

    }

   });

  

   GridBagLayoutDemo demo = new GridBagLayoutDemo();

   demo.init();

  

   frame.add(BorderLayout.CENTER, demo);

   frame.setSize(270, 150);

   //frame.setSize(361, 254);

   frame.setVisible(true);  

}

}

[CODE]

显示窗口可以如图所示划分为 5 行和 4 列,每个按钮所在的单元可以很容易的确定,所以 gridx 和 gridy 的值都很容易确定,组件放置在的位置也就确定了。

然而,组件都有一个默认的大小,它和它所在的单元格大小一般情况下都是不同的,并且如果窗口发生变化时,组件做出什么样的改变就要用到fill ,weightx 和 weighty了。

结合具体的例子来看:

设置 Button 1

//Button 1

Button btn1 = new Button("Button01");

//所在单元格为(0, 0)

constraints.gridx = 0;

constraints.gridy = 0;

//占用一个长度长和一个长度宽

constraints.gridwidth = 1;

constraints.gridheight = 1;

//这里使用默认的 anchor

constraints.anchor = GridBagConstraints.CENTER;

//设置NONE填充

constraints.fill = GridBagConstraints.NONE;

//设置填充权重

constraints.weightx = 0.0;

constraints.weighty = 0.0;

//Insets, ipadx, ipady使用默认值

constraints.insets = new Insets(0, 0, 0, 0);

constraints.ipadx = 0;

constraints.ipady = 0;

//添加 Button01 按钮

makeButton(gbaglayout, constraints, btn1);

 

设置 Button02, Button03, Button04  

//Button 2

Button btn2 = new Button("Button02");

constraints.gridx = 1;  

makeButton(gbaglayout, constraints, btn2);

//Button 3

Button btn3 = new Button("Button03");

constraints.gridx = 2;  

makeButton(gbaglayout, constraints, btn3);

//Button 4

Button btn4 = new Button("Button04");

constraints.gridx = 3;  

makeButton(gbaglayout, constraints, btn4);

Button02, Button03, Button04和Button01的区别是,只需要设置他们的gridx值;其他的不变。

 

设置 Button05

//Button 5

Button btn5 = new Button("Button05");

//所在单元格为(0, 1)

constraints.gridx = 0;

constraints.gridy = 1;

//占用了所有的4个单元格宽度 ,一个单元格高,省略没写

constraints.gridwidth = 4;

makeButton(gbaglayout, constraints, btn5);

 

设置 Button06

//Button 6

Button btn6 = new Button("Button06");

//所在单元格为(0, 2)

constraints.gridx = 0;

constraints.gridy = 2;

//占用了所有的3个单元格宽度 ,一个单元格高,省略没写

constraints.gridwidth = 3;

makeButton(gbaglayout, constraints, btn6);

 

设置 Button07

//Button 7

Button btn7 = new Button("Button07");

//所在单元格为(3, 2)

constraints.gridx = 3;

constraints.gridy = 2;

//占用了所有的1个单元格宽度 ,一个单元格高,省略没写

constraints.gridwidth = 1;

makeButton(gbaglayout, constraints, btn7);

 

设置 Button08

//Button 8

Button btn8 = new Button("Button08");

//所在单元格为(0, 3)

constraints.gridx = 0;

constraints.gridy = 3;

//占用了所有的1个单元格宽度 ,2个单元格高

constraints.gridwidth = 1;

constraints.gridheight = 2;   

makeButton(gbaglayout, constraints, btn8);

 

设置 Button09

//Button 9

Button btn9 = new Button("Button09");

//所在单元格为(1, 3)

constraints.gridx = 1;

constraints.gridy = 3;

//占用了所有的3个单元格宽度 ,1个单元格高

constraints.gridwidth = 3;

constraints.gridheight = 1;   

makeButton(gbaglayout, constraints, btn9);

 

设置 Button10

//Button 10

Button btn10 = new Button("Button10");

//所在单元格为(1, 4)

constraints.gridx = 1;

constraints.gridy = 4;

//占用了所有的3个单元格宽度 ,1个单元格高,由于和Button09一样,也可以不写

constraints.gridwidth = 3;

constraints.gridheight = 1;   

makeButton(gbaglayout, constraints, btn10);

设置好10个按钮之后,看看结果如何:

和目标差别还很大,但它确实是按照期望的方式,在特定的单元格内显示了。

[转载]布局管理器GridBagLayout的使用_第2张图片

为什么有差别呢?原因是单元格比按钮默认的大小要大,没有设置fill方式。

由于需要Button01, Button02, Button03 和 Button04的长度充满单元格,可修改 fill 属性为:constraints.fill = GridBagConstraints.HORIZONTAL;

同时改变constraints.weightx = 1.0;使得窗口变化时按钮能随之伸长(如果不改变weightx值,改变窗口大小时,看看会有什么效果)。

显示结果:

[转载]布局管理器GridBagLayout的使用_第3张图片

可以看到 Button08有问题,更改相应部分:

//Button 8

Button btn8 = new Button("Button08");

constraints.gridx = 0;

constraints.gridy = 3;

constraints.gridwidth = 1;

constraints.gridheight = 2;

//设置Button08的 fill 为 BOTH

constraints.fill = GridBagConstraints.BOTH;  

constraints.weightx = 1.0;

constraints.weighty = 1.0;

makeButton(gbaglayout, constraints, btn8);

//Button09

Button btn9 = new Button("Button09");

constraints.gridx = 1;

constraints.gridy = 3;

constraints.gridwidth = 3;

constraints.gridheight = 1;

//不需要垂直变化

constraints.fill = GridBagConstraints.HORIZONTAL;

constraints.weightx = 1.0;

constraints.weighty = 0.0;

makeButton(gbaglayout, constraints, btn9);

//Button 10

Button btn10 = new Button("Button10");

constraints.gridx = 1;

constraints.gridy = 4;

constraints.gridwidth = 3;

constraints.gridheight = 1;  

//需要垂直变化

constraints.fill = GridBagConstraints.BOTH;

constraints.weightx = 1.0;

constraints.weighty = 1.0;

makeButton(gbaglayout, constraints, btn10);

好了,所有的属性设置好了。

[转载]布局管理器GridBagLayout的使用_第4张图片

 

 

完整代码:

[CODE]

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class GridBagLayoutDemo extends Applet {

GridBagLayout gbaglayout;

GridBagConstraints constraints;

public void init() {

   gbaglayout = new GridBagLayout();  

   constraints = new GridBagConstraints();

   setLayout(gbaglayout);

  

   //Button 1

   Button btn1 = new Button("Button01");

   constraints.gridx = 0;

   constraints.gridy = 0;

   constraints.gridwidth = 1;

   constraints.gridheight = 1;

   constraints.anchor = GridBagConstraints.CENTER;  

   constraints.fill = GridBagConstraints.HORIZONTAL;

   constraints.weightx = 1.0;

   constraints.weighty = 0.0;

   constraints.insets = new Insets(0, 0, 0, 0);

   constraints.ipadx = 0;

   constraints.ipady = 0;

   makeButton(gbaglayout, constraints, btn1);

  

  //Button 2

   Button btn2 = new Button("Button02");

   constraints.gridx = 1;  

   makeButton(gbaglayout, constraints, btn2);

  

   //Button 3

   Button btn3 = new Button("Button03");

   constraints.gridx = 2;  

   makeButton(gbaglayout, constraints, btn3);

  

   //Button 4

   Button btn4 = new Button("Button04");

   constraints.gridx = 3;  

   makeButton(gbaglayout, constraints, btn4);

  

   //Button 5

   Button btn5 = new Button("Button05");

   constraints.gridx = 0;

   constraints.gridy = 1;

   constraints.gridwidth = 4;

   makeButton(gbaglayout, constraints, btn5);

  

   //Button 6

   Button btn6 = new Button("Button06");

   constraints.gridx = 0;

   constraints.gridy = 2;

   constraints.gridwidth = 3;

   makeButton(gbaglayout, constraints, btn6);

  

   //Button 7

   Button btn7 = new Button("Button07");

   constraints.gridx = 3;

   constraints.gridy = 2;

   constraints.gridwidth = 1;

   makeButton(gbaglayout, constraints, btn7);

  

   //Button 8

   Button btn8 = new Button("Button08");

   constraints.gridx = 0;

   constraints.gridy = 3;

   constraints.gridwidth = 1;

   constraints.gridheight = 2;

   constraints.fill = GridBagConstraints.BOTH;

   constraints.weightx = 1.0;

   constraints.weighty = 1.0;    

   makeButton(gbaglayout, constraints, btn8);

  

//Button 9

   Button btn9 = new Button("Button09");

   constraints.gridx = 1;

   constraints.gridy = 3;

   constraints.gridwidth = 3;

   constraints.gridheight = 1;  

   constraints.fill = GridBagConstraints.HORIZONTAL;

   constraints.weightx = 1.0;

   constraints.weighty = 0.0;

   makeButton(gbaglayout, constraints, btn9);

  

  //Button 10

   Button btn10 = new Button("Button10");

   constraints.gridx = 1;

   constraints.gridy = 4;

   constraints.gridwidth = 3;

   constraints.gridheight = 1;  

   constraints.fill = GridBagConstraints.BOTH;

   constraints.weightx = 1.0;

   constraints.weighty = 1.0;

   makeButton(gbaglayout, constraints, btn10);

}

private void makeButton(GridBagLayout gbaglayout, GridBagConstraints constraints, Component component) {

   gbaglayout.setConstraints(component, constraints);

   add(component);

}

public static void main(String[] args) {

   Frame frame = new Frame("GridBagLayoutDemo");

   frame.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

     System.exit(0);

    }

   });

  

   GridBagLayoutDemo demo = new GridBagLayoutDemo();

   demo.init();

  

   frame.add(BorderLayout.CENTER, demo);

   frame.setSize(270, 150);

   //frame.setSize(361, 254);

   frame.setVisible(true);  

}

}

[CODE]

以下是 fill 和 anchor 的示例:

fill的4种方式:

 

anchor的9种方式:

你可能感兴趣的:(java,String,import,constraints,button,applet)