首页 > 代码库 > GridBagLayout练习

GridBagLayout练习

 摘自http://blog.csdn.net/qq_18989901/article/details/52403737

 GridBagLayout的用法

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 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 }

 

GridBagLayout练习