您的位置:首页 > 其它

GridBagLayout练习

2016-12-10 22:31 155 查看
摘自http://blog.csdn.net/qq_18989901/article/details/52403737

GridBagLayout的用法

GridBagLayout是面板设计中最复杂的布局工具,当然用的好的话也是最方便的。

GridBagLayout其组件的摆放都是有GridBagConstraints控制的。可以将需要设计的界面划分成许多个纵横的小网格,每个网格里面最多放置一个组件,一个组件可以占用多个网格。

网格的定义是由gridx和gridy定义的,前者表示横坐标,后者表示纵坐标,坐标是从0开始,从整个面板的左上角开始算起,所以在上图中,按钮7所在的位置gridx=0,gridy=0;以此类推,后面所有的网格位置都可以这样定义;

但是图中网格的宽度和高度是由什么定义的呢?答案是gridwidth和gridheight,为1表示占用一个网格,为2表示占用两个网格,如上图中"+"的gridwidth=1,gridheight=2;

知道这两个,就可以大体把网格划分清楚,每一个的具体位置也就有了着落。有朋友会说,我也是这样写的,但是组件怎么就那么小呢?那是因为GridBaglayout里面还有一项参数weight,她控制着组件随着面板的变化自身的变化情况,默认情况下weightx=0,weighty=0,意思是组件大小固定,不管面板如何变,自身就那么大,但是如果想让组件随面板变化的话,可以设置weightx和weighty,设置为浮点数也行,其值代表着变化的大小,也就是你宽度设为2,高度设为1的话,拉大面板会使组件越变越宽。

这三个是比较重要的Constraints,另外,还有insets,其有四个参数,表示其上左下右和相邻组件的最小间隔;ipadx和ipady表示组件间距,默认是0(其和insets不同读者可以自己摸索一下);fill参数表示当网格区域比组件大的伤害,组件是以何种方式填充区域,是全方位还是水平或竖直;anchor是区域比组件大时,组件应该显示在区域的哪个方位,西北还是东南;

测试1:

本文主要通过设计一个计算器界面的方式,来学习GridBagLayout的使用。最终的效果如下图所示:





下面是我实现面板显示部分的所有代码:

1 package com.wst.bj;
2
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6
7 import javax.swing.JButton;
8 import javax.swing.JFrame;
9 import javax.swing.JPanel;
10 import javax.swing.JTextField;
11
12 public class GridBagTest2 {
13
14     private JFrame jframe = new JFrame();
15     private JButton bt1 = new JButton("1");
16     private JButton bt2 = new JButton("2");
17     private JButton bt3 = new JButton("3");
18     private JButton bt4 = new JButton("4");
19     private JButton bt5 = new JButton("5");
20     private JButton bt6 = new JButton("6");
21     private JButton bt7 = new JButton("7");
22     private JButton bt8 = new JButton("8");
23     private JButton bt9 = new JButton("9");
24     private JButton bt0 = new JButton("0");
25     private JButton btdot = new JButton(".");
26     private JButton btsignleft = new JButton("(");
27     private JButton btsignright = new JButton(")");
28     private JButton btp = new JButton("+");
29     private JButton btm = new JButton("-");
30     private JButton btmp = new JButton("*");
31     private JButton btd = new JButton("/");
32     private JButton bte = new JButton("=");
33     private JButton bt = new JButton("00");
34     private JButton btclear = new JButton("cl");
35     private JTextField textField = new JTextField();
36
37     public GridBagTest2() {
38         init();
39     }
40
41     private void init()
42     {
43         FrameUtil.initFram(jframe, 300, 400);
44         JPanel jpanel = createPanel();
45         jframe.add(jpanel);
46     }
47
48
49     private JPanel createPanel(){
50         JPanel panel = new JPanel();
51
52         GridBagLayout gbl = new GridBagLayout();
53         GridBagConstraints gbs = new GridBagConstraints();
54         panel.setLayout(gbl);
55
56         panel.add(bt7);panel.add(bt8);panel.add(bt9);panel.add(btp);
57         panel.add(bt4);panel.add(bt5);panel.add(bt6);
58 //      panel.add(bt1);panel.add(bt2);panel.add(bt9);panel.add(btp);
59         panel.add(bt1);panel.add(bt2);panel.add(bt3);panel.add(btm);
60         panel.add(bt0);panel.add(btdot);
61         panel.add(btsignleft);panel.add(btsignright);panel.add(bte);panel.add(btmp);
62         panel.add(btclear);panel.add(bt);                panel.add(btd);
63         panel.add(textField);
64
65         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
66         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
67         gbs.gridx=0;gbs.gridy=0;
68         gbl.setConstraints(bt7, gbs);
69         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
70         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
71         gbs.gridx=1;gbs.gridy=0;
72         gbl.setConstraints(bt8, gbs);
73         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
74         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
75         gbs.gridx=2;gbs.gridy=0;
76         gbl.setConstraints(bt9, gbs);
77
78         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
79         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
80         gbs.gridx=3;gbs.gridy=0;
81         gbl.setConstraints(btp, gbs);
82
83         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
84         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
85         gbs.gridx=0;gbs.gridy=1;
86         gbl.setConstraints(bt4, gbs);
87         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
88         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
89         gbs.gridx=1;gbs.gridy=1;
90         gbl.setConstraints(bt5, gbs);
91         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
92         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
93         gbs.gridx=2;gbs.gridy=1;
94         gbl.setConstraints(bt6, gbs);
95
96         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
97         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
98         gbs.gridx=0;gbs.gridy=2;
99         gbl.setConstraints(bt1, gbs);
100         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
101         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
102         gbs.gridx=1;gbs.gridy=2;
103         gbl.setConstraints(bt2, gbs);
104         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
105         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
106         gbs.gridx=2;gbs.gridy=2;
107         gbl.setConstraints(bt3, gbs);
108
109         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
110         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
111         gbs.gridx=3;gbs.gridy=2;
112         gbl.setConstraints(btm, gbs);
113
114         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=2;gbs.gridheight=1;
115         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
116         gbs.gridx=0;gbs.gridy=3;
117         gbl.setConstraints(bt0, gbs);
118         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
119         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
120         gbs.gridx=2;gbs.gridy=3;
121         gbl.setConstraints(btdot, gbs);
122
123         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
124         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
125         gbs.gridx=0;gbs.gridy=4;
126         gbl.setConstraints(btsignleft, gbs);
127         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
128         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
129         gbs.gridx=1;gbs.gridy=4;
130         gbl.setConstraints(btsignright, gbs);
131         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=2;
132         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
133         gbs.gridx=2;gbs.gridy=4;
134         gbl.setConstraints(bte, gbs);
135         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
136         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
137         gbs.gridx=3;gbs.gridy=4;
138         gbl.setConstraints(btmp, gbs);
139         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
140         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
141         gbs.gridx=0;gbs.gridy=5;
142         gbl.setConstraints(btclear, gbs);
143         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
144         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
145         gbs.gridx=1;gbs.gridy=5;
146         gbl.setConstraints(bt, gbs);
147         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=1;gbs.gridheight=1;
148         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
149         gbs.gridx=3;gbs.gridy=5;
150         gbl.setConstraints(btd, gbs);
151
152         gbs.fill=GridBagConstraints.BOTH;gbs.gridwidth=4;gbs.gridheight=3;
153         gbs.insets=new Insets(5, 5, 5, 5);gbs.weightx=1;gbs.weighty=1;
154         gbs.gridx=0;gbs.gridy=6;
155         gbl.setConstraints(textField, gbs);
156
157         return panel;
158     }
159 }


测试2:



1 package com.wst.bj;
2
3
4 import java.awt.GridBagConstraints;
5 import java.awt.GridBagLayout;
6 import java.awt.Insets;
7
8 import javax.swing.JButton;
9 import javax.swing.JFrame;
10 import javax.swing.JPanel;
11 import javax.swing.JTextField;
12
13 public class GridBagTest4 {
14
15     private JFrame jframe = new JFrame();
16     private JButton bt1 = new JButton("1");
17     private JButton bt2 = new JButton("2");
18     private JButton bt3 = new JButton("3");
19     private JButton bt4 = new JButton("4");
20     private JButton bt5 = new JButton("5");
21     private JButton bt6 = new JButton("6");
22     private JButton bt7 = new JButton("7");
23     private JButton bt8 = new JButton("8");
24     private JButton bt9 = new JButton("9");
25     private JButton bt0 = new JButton("0");
26     private JButton btdot = new JButton(".");
27     private JButton btsignleft = new JButton("(");
28     private JButton btsignright = new JButton(")");
29     private JButton btp = new JButton("+");
30     private JButton btm = new JButton("-");
31     private JButton btmp = new JButton("*");
32     private JButton btd = new JButton("/");
33     private JButton bte = new JButton("=");
34     private JButton bt = new JButton("00");
35     private JButton btclear = new JButton("cl");
36     private JTextField textField = new JTextField();
37
38     public GridBagTest4() {
39         init();
40     }
41
42     private void init()
43     {
44         FrameUtil.initFram(jframe, 300, 400);
45         JPanel jpanel = createPanel();
46         jframe.add(jpanel);
47     }
48
49
50     private JPanel createPanel(){
51         JPanel panel = new JPanel();
52
53         GridBagLayout gbl = new GridBagLayout();
54         GridBagConstraints gbs = new GridBagConstraints();
55         panel.setLayout(gbl);
56
57         panel.add(bt7);panel.add(bt8);panel.add(bt9);panel.add(btp);
58         panel.add(bt4);panel.add(bt5);panel.add(bt6);
59         panel.add(bt1);panel.add(bt2);panel.add(bt3);panel.add(btm);
60         panel.add(bt0);panel.add(btdot);
61         panel.add(btsignleft);panel.add(btsignright);panel.add(bte);panel.add(btmp);
62         panel.add(btclear);panel.add(bt);                panel.add(btd);
63         panel.add(textField);
64
65         gbs.fill=GridBagConstraints.NONE;
66 //        gbs.gridwidth=1;gbs.gridheight=1;
67 //        gbs.insets=new Insets(5, 5, 5, 5);
68 //        gbs.ipadx = 100;
69 //        gbs.ipady = 100;
70         gbs.anchor = GridBagConstraints.WEST;
71         gbs.weightx=1;gbs.weighty=1;
72         gbs.gridx=0;gbs.gridy=0;
73         gbl.setConstraints(bt7, gbs);
74
75         gbs.fill=GridBagConstraints.NONE;
76 //        gbs.gridwidth=1;gbs.gridheight=1;
77 //        gbs.insets=new Insets(5, 5, 5, 5);
78 //        gbs.weightx=1;gbs.weighty=1;
79         gbs.anchor = GridBagConstraints.NORTHEAST;
80         gbs.gridx=1;gbs.gridy=0;
81         gbl.setConstraints(bt8, gbs);
82
83         gbs.fill=GridBagConstraints.NONE;
84 //        gbs.gridwidth=1;gbs.gridheight=1;
85 //        gbs.insets=new Insets(5, 5, 5, 5);
86 //        gbs.weightx=1;gbs.weighty=1;
87         gbs.anchor = GridBagConstraints.SOUTH;
88         gbs.gridx=2;gbs.gridy=0;
89         gbl.setConstraints(bt9, gbs);
90 //
91         gbs.fill=GridBagConstraints.BOTH;
92         gbs.gridwidth=1;gbs.gridheight=2;
93 ////        gbs.insets=new Insets(5, 5, 5, 5);
94 ////        gbs.weightx=1;gbs.weighty=1;
95         gbs.gridx=3;gbs.gridy=0;
96         gbl.setConstraints(btp, gbs);
97         // =====================================================
98         gbs.fill=GridBagConstraints.VERTICAL;
99         gbs.gridwidth=1;gbs.gridheight=1;
100 //        gbs.insets=new Insets(5, 5, 5, 5);
101 //        gbs.weightx=1;gbs.weighty=1;
102         gbs.anchor = GridBagConstraints.WEST;
103         gbs.gridx=0;gbs.gridy=1;
104         gbl.setConstraints(bt4, gbs);
105 //
106         gbs.fill=GridBagConstraints.VERTICAL;
107         gbs.gridwidth=1;gbs.gridheight=1;
108 //        gbs.insets=new Insets(5, 5, 5, 5);
109 //        gbs.weightx=1;gbs.weighty=1;
110         gbs.anchor = GridBagConstraints.SOUTHEAST;
111         gbs.gridx=1;gbs.gridy=1;
112         gbl.setConstraints(bt5, gbs);
113
114         gbs.fill=GridBagConstraints.VERTICAL;
115         gbs.gridwidth=1;gbs.gridheight=1;
116 //        gbs.insets=new Insets(5, 5, 5, 5);
117 //        gbs.weightx=1;gbs.weighty=1;
118         gbs.anchor = GridBagConstraints.CENTER;
119         gbs.gridx=2;gbs.gridy=1;
120         gbl.setConstraints(bt6, gbs);
121         // =====================================================
122         gbs.fill=GridBagConstraints.HORIZONTAL;
123         gbs.gridwidth=1;gbs.gridheight=1;
124 //        gbs.insets=new Insets(5, 5, 5, 5);
125 //        gbs.weightx=1;gbs.weighty=1;
126         gbs.anchor = GridBagConstraints.NORTH;
127         gbs.gridx=0;gbs.gridy=2;
128         gbl.setConstraints(bt1, gbs);
129
130         gbs.fill=GridBagConstraints.HORIZONTAL;
131         gbs.gridwidth=1;gbs.gridheight=1;
132 //        gbs.insets=new Insets(5, 5, 5, 5);
133 //        gbs.weightx=1;gbs.weighty=1;
134         gbs.anchor = GridBagConstraints.SOUTHEAST;
135         gbs.gridx=1;gbs.gridy=2;
136         gbl.setConstraints(bt2, gbs);
137
138         gbs.fill=GridBagConstraints.HORIZONTAL;
139         gbs.gridwidth=1;gbs.gridheight=1;
140 //        gbs.insets=new Insets(5, 5, 5, 5);
141 //        gbs.weightx=1;gbs.weighty=1;
142         gbs.anchor = GridBagConstraints.CENTER;
143         gbs.gridx=2;gbs.gridy=2;
144         gbl.setConstraints(bt3, gbs);
145
146         gbs.fill=GridBagConstraints.BOTH;
147         gbs.gridwidth=1;gbs.gridheight=2;
148 //        gbs.insets=new Insets(5, 5, 5, 5);
149 //        gbs.weightx=1;gbs.weighty=1;
150         gbs.gridx=3;gbs.gridy=2;
151         gbl.setConstraints(btm, gbs);
152         // =====================================================
153         gbs.fill=GridBagConstraints.BOTH;
154         gbs.gridwidth=2;gbs.gridheight=1;
155 //        gbs.insets=new Insets(5, 5, 5, 5);
156 //        gbs.weightx=1;gbs.weighty=1;
157         gbs.gridx=0;gbs.gridy=3;
158         gbl.setConstraints(bt0, gbs);
159
160         gbs.fill=GridBagConstraints.BOTH;
161         gbs.gridwidth=1;gbs.gridheight=1;
162 //        gbs.insets=new Insets(5, 5, 5, 5);
163 //        gbs.weightx=1;gbs.weighty=1;
164         gbs.gridx=2;gbs.gridy=3;
165         gbl.setConstraints(btdot, gbs);
166         // =====================================================
167         gbs.fill=GridBagConstraints.BOTH;
168         gbs.gridwidth=1;gbs.gridheight=1;
169 //        gbs.insets=new Insets(5, 5, 5, 5);
170 //        gbs.weightx=1;gbs.weighty=1;
171         gbs.gridx=0;gbs.gridy=4;
172         gbl.setConstraints(btsignleft, gbs);
173
174         gbs.fill=GridBagConstraints.BOTH;
175         gbs.gridwidth=1;gbs.gridheight=1;
176         gbs.insets=new Insets(7, 7, 2, 2);
177 //        gbs.weightx=1;gbs.weighty=1;
178         gbs.gridx=1;gbs.gridy=4;
179         gbl.setConstraints(btsignright, gbs);
180
181         gbs.fill=GridBagConstraints.BOTH;
182         gbs.gridwidth=1;gbs.gridheight=2;
183         gbs.insets=new Insets(0, 0, 0, 0);
184 //        gbs.weightx=1;gbs.weighty=1;
185         gbs.gridx=2;gbs.gridy=4;
186         gbl.setConstraints(bte, gbs);
187
188         gbs.fill=GridBagConstraints.BOTH;
189         gbs.gridwidth=1;gbs.gridheight=1;
190 //        gbs.insets=new Insets(5, 5, 5, 5);
191 //        gbs.weightx=1;gbs.weighty=1;
192         gbs.gridx=3;gbs.gridy=4;
193         gbl.setConstraints(btmp, gbs);
194         // =====================================================
195         gbs.fill=GridBagConstraints.BOTH;
196         gbs.gridwidth=1;gbs.gridheight=1;
197 //        gbs.insets=new Insets(5, 5, 5, 5);
198         gbs.weightx=0;gbs.weighty=0;
199         gbs.gridx=0;gbs.gridy=5;
200         gbl.setConstraints(btclear, gbs);
201
202         gbs.fill=GridBagConstraints.BOTH;
203         gbs.gridwidth=1;gbs.gridheight=1;
204 //        gbs.insets=new Insets(5, 5, 5, 5);
205 //        gbs.weightx=1;gbs.weighty=1;
206         gbs.gridx=1;gbs.gridy=5;
207         gbl.setConstraints(bt, gbs);
208
209         gbs.fill=GridBagConstraints.NONE;
210         gbs.gridwidth=1;gbs.gridheight=1;
211 //        gbs.insets=new Insets(5, 5, 5, 5);
212 //        gbs.weightx=1;gbs.weighty=1;
213         gbs.anchor = GridBagConstraints.EAST;
214         gbs.gridx=3;gbs.gridy=5;
215         gbl.setConstraints(btd, gbs);
216         // =====================================================
217         gbs.fill=GridBagConstraints.BOTH;
218         gbs.gridwidth=4;gbs.gridheight=3;
219 //        gbs.insets=new Insets(5, 5, 5, 5);
220         gbs.weightx=1;gbs.weighty=1;
221         gbs.gridx=0;gbs.gridy=6;
222         gbl.setConstraints(textField, gbs);
223 //        // =====================================================
224         gbs.anchor = GridBagConstraints.NORTH;
225         return panel;
226     }
227 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: