Java 的布局管理器GridBagLayout的使用方法【图文说明】

GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的,

GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同便可以将组件垂直、水平或沿它们的基线对齐。

每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,该单元被称为显示区域

下面就通过一个记事本案例去说明GridBagLayout的使用方法。

 

分析:

带有箭头的说明可以拉伸的。

4占用4个格子,6占用4个格子。如果设置6可以拉伸了,那么4也会跟着拉伸。

但是如果设置4拉伸,那么7所在的列也可以拉伸,所以4不能设置拉伸。我们应该设置4是跟随6进行拉伸。

灰色的线是为了看清布局的大概,组件占用的格子数。

Java 的布局管理器GridBagLayout的使用方法【图文说明】

运行时的显示效果

Java 的布局管理器GridBagLayout的使用方法【图文说明】

 

 

 

 1 import java.awt.*;
 2 import javax.swing.*;
 3 
 4 public class GridBagDemo extends JFrame {
 5     public static void main(String args[]) {
 6         GridBagDemo demo = new GridBagDemo();
 7     }
 8 
 9     public GridBagDemo() {
10         init();
11         this.setSize(600,600);
12         this.setVisible(true);
13     }
14     public void init() {
15         j1 = new JButton("打开");
16         j2 = new JButton("保存");
17         j3 = new JButton("另存为");
18         j4 = new JPanel();
19         String[] str = { "java笔记", "C#笔记", "HTML5笔记" };
20         j5 = new JComboBox(str);
21         j6 = new JTextField();
22         j7 = new JButton("清空");
23         j8 = new JList(str);
24         j9 = new JTextArea();
25      j9.setBackground(Color.PINK);//为了看出效果,设置了颜色
26         GridBagLayout layout = new GridBagLayout();
27         this.setLayout(layout);
28         this.add(j1);//把组件添加进jframe
29         this.add(j2);
30         this.add(j3);
31         this.add(j4);
32         this.add(j5);
33         this.add(j6);
34         this.add(j7);
35         this.add(j8);
36         this.add(j9);
37         GridBagConstraints s= new GridBagConstraints();//定义一个GridBagConstraints,
38         //是用来控制添加进的组件的显示位置
39         s.fill = GridBagConstraints.BOTH;
40         //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况
41         //NONE:不调整组件大小。
42         //HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度。
43         //VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。
44         //BOTH:使组件完全填满其显示区域。
45         s.gridwidth=1;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个
46         s.weightx = 0;//该方法设置组件水平的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
47         s.weighty=0;//该方法设置组件垂直的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
48         layout.setConstraints(j1, s);//设置组件
49         s.gridwidth=1;
50         s.weightx = 0;
51         s.weighty=0;
52         layout.setConstraints(j2, s);
53         s.gridwidth=1;
54         s.weightx = 0;
55         s.weighty=0;
56         layout.setConstraints(j3, s);
57         s.gridwidth=0;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个
58         s.weightx = 0;//不能为1,j4是占了4个格,并且可以横向拉伸,
59         //但是如果为1,后面行的列的格也会跟着拉伸,导致j7所在的列也可以拉伸
60         //所以应该是跟着j6进行拉伸
61         s.weighty=0;
62         layout.setConstraints(j4, s)
63         ;s.gridwidth=2;
64         s.weightx = 0;
65         s.weighty=0;
66         layout.setConstraints(j5, s);
67         ;s.gridwidth=4;
68         s.weightx = 1;
69         s.weighty=0;
70         layout.setConstraints(j6, s);
71         ;s.gridwidth=0;
72         s.weightx = 0;
73         s.weighty=0;
74         layout.setConstraints(j7, s);
75         ;s.gridwidth=2;
76         s.weightx = 0;
77         s.weighty=1;
78         layout.setConstraints(j8, s);
79         ;s.gridwidth=5;
80         s.weightx = 0;
81         s.weighty=1;
82         layout.setConstraints(j9, s);
83     }
84     JButton j1;
85     JButton j2;
86     JButton j3;
87     JPanel j4;
88     JComboBox j5;
89     JTextField j6;
90     JButton j7;
91     JList j8;
92     JTextArea j9;
93 }