Fork me on GitHub

java布局管理器

java awt 布局管理器

FlowLayout布局管理器

在FlowLayout布局管理器中组件像水流一样向某方向流动,遇到边界就会折回,重头开始排列。默认从左往右排列。Panel和Applet默认使用FlowLayout布局管理器。

FlowLayout有三个构造器:

  • FlowLayout(): 使用默认对齐方式、默认垂直、水平间距创建FlowLayout布局管理器。

  • FlowLayout(int align): 使用指定对齐方式、默认垂直、水平间距创建FlowLayout布局管理器。

  • FlowLayout(int align,int hgap,int vgap): 使用指定对齐方式、指定垂直、水平间距创建FlowLayout布局管理器。

align表明FlowLayout中组件的排列方向,该参数使用FlowLayout类的静态常量:FlowLayout.LEFT、FlowLayout.CENTER、FlowLayout.RIGHT。

BorderLayout布局管理器

BorderLayout将容器分成EAST、SOUTH、WEST、NORTH、CENTER五个区域,不同组件可以被放置在任意区域。

  • 当像容器中添加组件时,需要指定添加到哪个区域。默认添加到中间区域。
  • 当向某个区域添加多个组件时,后加入的组件会覆盖前面的组件。

Frame、Dialog、ScrollPane默认使用BorderLayout布局管理器,BorderLayout有两个构造器:

  1. BorderLayout():使用默认水平间距、垂直间距创建BorderLayout布局管理器;
  2. BorderLayout(int hgap,int vgap):使用指定的水平间距、垂直间距创建BorderLayout布局管理器。

BorderLayout布局管理器使用几个静态属性来指定添加到哪个区域,BorderLayout有如下几个静态变量:EAST、NORTH、WEST、SOUTH、CENTER。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BorderLayout_text {
public static void main(String[] args) {

Frame f = new Frame("测试窗口");
f.setLayout(new BorderLayout(30,5));
f.add(new Button("南"),BorderLayout.SOUTH);
f.add(new Button("北"),BorderLayout.NORTH);
f.add(new Button("中"));
f.add(new Button("东"),BorderLayout.EAST);
f.add(new Button("西"),BorderLayout.WEST);
f.add(new Button("中1"));
f.pack();
f.setVisible(true);


}

}

GridLayout布局管理器

GridLayout布局管理器将容器分割成纵横线分割的网络,每个网络所占的区域大小相同。添加组件时,默认从左往右、从上向下依次添加到每个网格中。与FlowLayout不同的是,放在GridLayout布局管理器中的个组件的大小由组件所处的区域来决定。
GridLayout有如下两个构造器:

  1. GridLayout(int rows,int cols):采用指定行数、列数、默认横向间距、纵向间距将容器分割成多个网络。
  2. GridLayout(int rows,int cols,int hgap,int vgap):采用指定行数、列数、指定横向间距、纵向间距将容器分割成多个网络。

结合BorderLayout和GridLayout开发的计算器可视窗口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

public class GridLayout_text {
public static void main(String[] args) {

Frame f = new Frame("计算器");
Panel p1 = new Panel();
p1.add(new TextField(30));
f.add(p1,BorderLayout.NORTH);
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);
}
}

GridBagLayout布局管理器

GridBagLayout布局管理器是最复杂,功能最强大的布局管理器,与GridLayout布局管理器不同的是:在GridBagLayout布局管理器中,一个组件可以跨越一或多个网格,并且可以设置个网格的大小互不相同,从而增加了布局的灵活性。当窗口的大小发生变化时,GridBagLayout布局管理器可以准确地控制窗口各部分的反应。
为了处理GridBagLayout中GUI组件的大小、跨越性,Java提供了GridBagConstraints对象,该对象与特定的GUI组件关联,用于控制GUI的大小、跨越性。
使用GridBagLayout布局管理器的步骤如下:
1. 创建GridBagLayout布局管理器,并指定GUI容器使用该布局管理器,如下代码:

1
2
GridBagLayout gb = new GridBagLayout();
contrainer.setLayout(gb);

2. 创建GridBagConstraints对象,并设置该对象的相关属性。如下代码:

1
2
3
4
5
GridBagConstrains gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;

3. 调用GridBagLayout对象的方法来建立GridBagConstraints对象和受控组件之间的联系。如下代码:

1
gb.setConstraints(c,gbc); //设置c组件收gbc对象控制。

4. 添加组件,如下代码:

1
constrainer.add(c);

重复2、 3、 4添加多个组件。

主要通过GridBagConstraints类控制GUI,该类有以下方法:

  • gridx、gridy: 设置受该对象控制的GUI组件的左上角所在网格的横向索引、纵向索引。这两个值还可以是默认值GridBagConstraints.RELATIVE(默认值),它表明当前组件紧跟上一个组件。

  • gridwidth、gridheight:设置GUI组件横向、纵向跨越多少个网格,默认值为1。
    若为GridBagConstraints.REMAINDER,表明GUI是横向、纵向最后一个组件。
    若为GridBagConstraints.RELATIVE,表明GUI时横向纵向倒数第二个组件。

  • fill:设置受该对象控制的GUI如何占领空白区域。该属性可以接受如下几个属性值:

    GridBagConstraints.NONE:GUI组件不扩大。
    GridBagConstraints.HORIZONTAL: GUI组件水平扩大占领空白区域。
    GridBagConstraints.VERTICAL: GUI组件垂直扩大占领空白区域。
    GridBagConstraints.BOTH: GUI组件水平、垂直扩大占领空白区域。

  • ipadx、ipady:设置GUI组件横向、纵向内部填充大小。

  • insets: 设置GUI组件的外部填充大小。

  • anchor:设置GUI组件在其显示区域之中的定位方式,有以下几种:GridBagConstraints.CENTER、GridBagConstraints.NORTH、GridBagConstraints.NORTHWEST、GridBagConstraints.NORTHEAST、GridBagConstraints.SOUTH、GridBagConstraints.SOUTHEAST、GridBagConstraints.SOUTHWEST、GridBagConstraints.EAST、GridBagConstraints.WEST。

  • weightx、weighty: 设置GUI组件占领多余空间的水平、垂直增加比例,默认值为0,表示不会扩大。

演示代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class GridBagLayout_text{
public static void main(String[] args)
{
Frame f = new Frame();
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbs = new GridBagConstraints();
f.setLayout(gb);

Button b1=new Button("b1");
gbs.fill=GridBagConstraints.BOTH;
gbs.gridwidth=GridBagConstraints.REMAINDER;
gbs.gridheight=1;
gbs.weightx=1;
gbs.ipadx=50;
gbs.ipady=50;
gb.setConstraints(b1, gbs);
f.add(b1);

Button b2=new Button("b2");
Button b3=new Button("b3");
Button b4=new Button("b4");
gbs.gridwidth=1;
gbs.gridheight=1;
gbs.weightx=1;
gbs.weighty=0;
gbs.ipadx=10;
gbs.ipady=10;
gbs.insets=new Insets(10,20,30,40);
gb.setConstraints(b2, gbs);
gb.setConstraints(b3, gbs);
gb.setConstraints(b4, gbs);
f.add(b2);
f.add(b3);
f.add(b4);
f.pack();
f.setVisible(true);
}

}

CardLayout布局管理器

CardLayout布局管理器以时间而非空间来管理它里面的组件,它将加入容器的所有组件看做一叠卡片,每次只有崔上面的那个Componet才可见。
CardLayout提供了两个构造器:

  1. CardLayout(): 创建默认的CardLayout布局管理器。
  2. CardLayout(intnhgap,int vgap): 通过指定卡片与容器左右边界的间距(hgap),上下边界的间距(vgap)来创建布局管理器。
    CardLayout控制组件的常见方法:
  • first(Container target): 显示target容器中的第一张卡片。
  • last(Container target): 显示target容器中的最后一张卡片。
  • previous(Container target): 显示target容器中前一张卡片。
  • next(Container target): 显示target容器中后一张卡片。
  • show(Container target,String 那么): 显示target容器中指定名字的卡片。

演示代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

public class CardLayout_text {
Frame f = new Frame("测试窗口");
String[] name = {"第一张","第二张","第三张","第四张","第五张"};
Panel pl = new Panel();
CardLayout c = new CardLayout();
public void init()
{
pl.setLayout(c);
for(int i=0;i<name.length;i++)
{
pl.add(name[i],new Button(name[i]));
}
Panel p = new Panel();
Button previous = new Button("上一张");
previous.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
c.previous(pl);
}
});
Button next = new Button("下一张");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
c.next(pl);
}
});
Button first = new Button("第一张");
first.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
c.first(pl);
}
});
Button last = new Button("最后一张");
last.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
c.last(pl);
}
});
Button third = new Button("第三张");
third.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
c.show(pl,"第三张");
}
});
p.add(previous);
p.add(next);
p.add(first);
p.add(last);
p.add(third);
f.add(pl);
f.add(p,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}

public static void main(String[] args)
{

newCardLayout_text().init();

}
}

绝对定位

  1. 将Container的布局管理器设置为null:setLayout(null);
  2. 先调用 setBounds()或setSize()方法来先设置组建的大小、位置。或者直接创建GUI组件时通过构造咱叔指定该组件的大小、位置,然后添加到容器中。

    演示代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class text{

Frame f = new Frame("测试窗口");
Button b1 = new Button("第一个按钮");
Button b2 = new Button("第二个按钮");
public void init()
{
f.setLayout(null);
b1.setBounds(20,30,90,28);
f.add(b1);
b2.setBounds(50,45,120,35);
f.add(b2);
f.setBounds(50,50,200,100);
f.setVisible(true);
}
public static void main(String[] args)
{
new text().init();
}


}

BoxLayout布局管理器

BoxLayout布局管理器保留了GridBagLayout的优点,但没有那么复杂,BoxLayout布局管理器可以在垂直和水平两个方向上拜访GUI组件,BoxLayout提供了一个简单的构造器:

  • BoxLayout(Container target,int axis): 指定创建基于target容器的BoxLayout布局管理器,该布局管理器里组件按axis方向排列。axis可以为BoxLayout.X_AXIS(横向)和BoxLayout.Y_AXIS(纵向)两个方向。
    代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BoxLayout_text{
private Frame f = new Frame("测试");
public void init()
{
f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
f.add(new Button("第一个按钮"));
f.add(new Button("按钮二"));
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new BoxLayout_text().init();
}
}