AWT的几种布局管理器

AWT中常用的布局管理器有如下几个:FlowLayout, BorderLayout, GridLayout, GridBagLayout, CardLayout,Swing还提供了一个BoxLayout

FlowLayout从左向右排列所有组件,遇到边界就会折回下一行从新开始。它有三个构造器FlowLayout()FlowLayout(int align) FlowLayout(int align, int hgap, int vgap),其中的hgapvgap代表水平间距和垂直间距,align指的是组件的排列方向(从左向右,从右向左,从中间向两边),我们可以使用FlowLayout的静态常量来设置这个参数:FlowLayout.LEFT,FlowLayout.CENTER,FlowLayout.RIGHT。

 

BorderLayout将容器分为EASTSOUTHWESTNORTHCENTER五个区域,如下图所示:

我们在向使用此布局管理器的容器中添加组件时,需要制定添加到的区域,否则就默认添加到中间区域里,而当我们向一个区域添加多个组件时,后放入的组件会覆盖前面的组件。BorderLayout有两个构造器,BorderLayout()BorderLayout(int hgap,int vgap),hgapvgap代表的水平间距和垂直间距。我们在指定组件添加到的区域时,可以使用它的静态常量:BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.CENTER例如:

Frame f = new Frame();
f.setLayout(new BorderLayout(5,5));
f.add(new Button(“南”),SOUTH);//将一个按钮添加到南的位置

BorderLayout最多只能放5个组件,但是实际上我们可以先在Panel中添加多个组件,再将Panel添加到BorderLayout布局管理器中,因此我们实际可以放的组件要远远超过5个。

 

GridLayout将容器分割成大小相同的网格,我们在添加组件时将默认从左到右从上到下,将组件依次添加到每个网格中,而每个组件的大小也就由其所添加到的网格的大小所决定。 GridLayout同样也有两个构造器,GridLayout(int rows,int cols)GridLayout(int rows ,int cols,int hgap,int vgap),使用GridLayout的典型例子就是计算器的窗口:

import java.awt.*;
public class calculator
{
    public static void main(String[] args)
    {
        Frame f = new Frame("计算器");
        Panel p1 = new Panel();
        p1.add(new TextField(30));
        f.add(p1,BorderLayout.NORTH);
        
        //设置p2采用GridLayout布局管理器
        Panel p2 = new Panel();
        p2.setLayout(new GridLayout(3,5,4,4));
        String[] name = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","."};
        for(int i=0;i<name.length;i++)
        {
            p2.add(new Button(name[i]));
        }
        f.add(p2);//默认添加到中间
        f.pack();// 设置窗口为最佳大小
        f.setVisible(true);
    }
}

运行结果如图:

 

CardLayout将加入容器的所有组件看成一叠卡片,每次只有最上面的那个Component才可见,它有两个构造器CardLayout()CardLayout(int hgap, int vgap),有五个方法用来控制其中的组件:

first(Container target);//显示target容器中的第一张卡片

last(Container target);

previous(Container target);

next(Container target);

show(Container target,String name);//显示targer容器中指定名字的卡片

例子:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculator
{
    Frame f;
    Panel p1;
    Panel p2;
    String[] name = {"1","2","3","4","5"};
    CardLayout c;
    
    public void init()
    {    
        f = new Frame("yz");
        p1 = new Panel();
        p2 = new Panel();
        c = new CardLayout();
        
        p1.setLayout(c);
        for(int i=0;i<name.length;i++)
        {
            p1.add(name[i],new Button(name[i]));
        }
        
        //控制显示上一张的按钮
        Button previous = new Button("上一张");
        previous.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c.previous(p1);
            }
        });
        //控制显示下一张的按钮
        Button next = new Button("下一张");
        next.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c.next(p1);
            }
        });
        //控制显示第一张的按钮
        Button first = new Button("第一张");
        first.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c.first(p1);
            }
        });
        //控制显示最后一张的按钮
        Button last = new Button("最后一张");
        last.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c.last(p1);
            }
        });
        //根据card名显示的按钮
        Button third = new Button("第三张");
        third.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c.show(p1,"3");
            }
        });
        p2.add(previous);
        p2.add(next);
        p2.add(first);
        p2.add(last);
        p2.add(third);
        f.add(p1);//默认添加到中间
        f.add(p2,BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args)
    {
        new calculator().init();
    }
}

 

GridBagLayout是功能最强大也是最复杂的布局管理器,添加到其中的组件可以横跨一个或多个网格,并可以设置各网格的大小各不相同,当窗口大小发生变化时,其也可以准确的控制窗口各部分的反应。为了处理GridBagLayout组件的大小和跨越性,我们还需要一个GridBagConstraints对象,用这个对象与特定的组件相关联,来控制组件的大小和跨越性。在使用GridBagLayout时一般需要4步:

1.  创建GridBagLayout,并指定容器使用该布局管理器

GridBagLayout gb = new GridBagLayout();

container.setLayout(gb);

2.  创建GridBagConstraints的对象,并设置该对象的相关属性

         GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx=2;

gbc.gridy=1;

gbc.gridwidth=2;

gbc.gridheight=1;

3.       调用GridBagLayout对象的方法来建立GridBagConstraints对象与受控制组件之间的联系。

gb.setConstraints(c,gbc);//设置c组件受gbc控制

4.       添加组件

  container.add(c);

通常我们可以将234步写成一个addComponent方法,为这个方法传递所需要的参数,来进行添加组件的化简。例如:

public void addComponent(Component c, int gx, int gy, int gw,int gh)
{
    this.gridx=gx;
    this.gridy=gy;
    this.gridwidth=gw;
    this.gridheight=gh;
    gb.setConstraints(c,gbc);
    container.add(c);
}

使用GridBagLayout关键在于GridBagConstraints,该类具有如下几个方法:

fill:设置组件如何占领空白区域,它可取如下几个值:GridBagConstraints.NONE, GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL, GridBagConstraints.BOTH

gridx,gridy:设置组件的左上角所在网格的索引(网格的索引从0 开始),此外这两个值还可以设为GridBagConstraints.RELATIVE,这个值也是默认值,它表明当前组件紧跟在上一个组件之后。

gridwidhtgridheight:设置组件横向纵向跨越多少个网格,他们的默认值都是1,如果该组件是横向或纵向的最后一个还可以将此值设为GridBagConstraints.REMAINDER,若为倒数第二个组件则可以设值为GridBagConstraints.RELATIVE

ipadxipady:设置组件横向纵向的内部填充大小,即在组件的最小尺寸上还需要增大多少,若设置了这个值则组件在最小尺寸的基础上增大ipadx*2 ipady*2像素。

weightxweightydouble类型):就是权重,也就是组件组件占领多余空间的水平或垂直增加比例,默认值为0也就是不占领多余空间。例如有三个组件,我们将他们的水平增加比例分别设为1.0,2.0,3.0,当容器宽度增加60像素时,他们分别增加10,2030像素。如果我们希望某个组件的大小会随着容器的变化而变化,我们需要同时设置fillweightxweighty属性。

 

Swing中的BoxLayout布局管理器提供了一个构造器:BoxLayout(Container targer,int axis),它制定创建基于targer容器的BoxLayout布局管理器,它里面的组件按axis方向排列,axisBoxLayout.X_AXISBoxLayout.Y_AXIS两个方向。BoxLayout通常和Box容器结合使用,Box容器有点像Panel,它默认使用BoxLayout布局管理器。Box有两个静态方法来创建Box对象:createHorizontalBox()createVerticalBox(),一旦获得了Box容器之后,就可以使用Box来承装普通GUI组件,然后再将这些Box组件添加到其他容器中,从而形成整体的窗口布局。例如:

 

public class Test
{
    private Frame f = new Frame("cs");
    private Box horizontal = Box.createHorizontalBox();
    private Box vertical = Box.createVerticalBox();
    public void init()
    {
        horizontal.add(new Button("shuiping1"));
        horizontal.add(new Button("shuiping2"));
        vertical.add(new Button("chuizhi1"));
        vertical.add(new Button("chuizhi2"));
        f.add(horizontal, BorderLayout.NORTH);
        f.add(vertical);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args)
    {
        new Test().init();
    }
}

 

最后我们还可以使用绝对定位:只需要将Container的布局管理器设为null即可,也就是setLayout(null),往容器中加组件的时候指定组件的位置和大小。一般调用setBounds(int x,int y,int width,int height)

 

 

 

 

 

posted @ 2012-04-19 09:33  xiaoz_  阅读(7376)  评论(0编辑  收藏  举报