GridBagLayout使用深入浅出

上传人:宝路 文档编号:3258324 上传时间:2017-08-01 格式:DOC 页数:6 大小:182KB
返回 下载 相关 举报
GridBagLayout使用深入浅出_第1页
第1页 / 共6页
GridBagLayout使用深入浅出_第2页
第2页 / 共6页
GridBagLayout使用深入浅出_第3页
第3页 / 共6页
GridBagLayout使用深入浅出_第4页
第4页 / 共6页
GridBagLayout使用深入浅出_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《GridBagLayout使用深入浅出》由会员分享,可在线阅读,更多相关《GridBagLayout使用深入浅出(6页珍藏版)》请在金锄头文库上搜索。

1、GridBagLayout 是 JAVA 中一种最为复杂的布局管理器,但最为灵活,可以实现其他大多数布局管理器的功能。适合 Swing 开发者学习,新手建议先学习其他诸如Flowlayout/Bordlayout/CardLayout/Gridlayout 等管理器的使用,但这些管理器有着明显的不足和缺陷,往往需要配合使用,但需要划分 Jpanle 增加了复杂度。例如这一个计算器的界面,就必须用 GridBagLayout 来做才能有理想的效果。如果光使用 Gridlayout,要划分 Jpanle,而且还要把父容器布局设置为 null,但这样当父容器的大小改变时,这些 Jbutton 大小不

2、能改变,因此只有 GridBagLayout 才能胜任了。关于 GridBagConstraints 中的各个字段的作用我不想再说明了,麻烦自行查阅,我只说明思路。虽然代码有点长,但结构简单。首先把你想要的样子构建在脑子中或描绘于纸上例如上图,我用线来划分从左到右扫描,每遇到一个组件算一个单元格(虽然最上面只有一个 JtextField 对象,但下面有更小的 Jbutton 对象,因此一旦遇到 Jbutton 就划分了) 。这样标号,共有 5 列。从上到下扫描,每遇到一个组件算一个单元格。这样标号,共有 7 列。在代码中,当我们把 JtextField 添加进去的时候代码这样写:“add(ne

3、w JTextField(), new GridBagConstraints(0, 0, 5, 1, 100, 30, anchor, fill, insets, 300, 50);”,解释一下,设置为 GridBagLayout 的容器必须使用 add(Component comp, Object constraints)来添加组件,因此,关键难点就在于 GridBagConstraints 的参数填写了。GridBagConstraints(0, 0, 5, 1, 100, 30, anchor, fill, insets, 300, 50)的第一个参数表示该组件放置于第几列(从 0算起)

4、 ;第二个参数表示该组件放置于第几行(从 0算起) ;第三个参数表示该组件占几列;第四个参数表示该组件占几行;第五个参数表示该组件占 x轴总宽度的比例,注意是比例,由于这个组件已经占满了所有列,因此我填 100(这 100就相当于 100%了,因此下面只占 1 列的组件必须只能填 20,因为 5*20=100) ;第六个参数同理;第七个参数表示当组件的大小大于单元格的大小时如何缩小组件(此参数我验证未成功,只知道用 Centre) ;第八个参数表示组件大小小于单元格的大小时 如何扩充组件(用Both 表示横纵都扩充) ;第九个参数我尚未验证成功,但一直用 insets 无碍;最后两个参数指示组

5、件的最小尺寸,即当父容器缩小时,组件不会小于该尺寸,300 表示 x 轴,50 表示 y 轴。因此很容易就把参数填上去了。读者自行理解其他组件的 GridBagConstraints 参数。像“0”这个 Jbutton,需要占两个 Jbutton 的宽度(即要占 2 列) ,所以它的第三个参数写 2(如红字)GridBagConstraints(0, 6, 2, 1, 20, 20, anchor, fill, insets, 20, 20);。像“=”这个 Jbutton,他需要两个 jbutton 需要两个高度所以它的第四个参数填2。需要注意的是,这样的话组件之间没有间隔,不像 Gridl

6、ayout 一样可以指定间隔,其实这个也可以,但我使用起来有错误,网上关于这类的文章几乎没有,全靠我自己摸索。第五第六个参数仅当最后两个参数为 0 的时候生效(初步验证)完整代码如下:import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.J

7、TextField;public class GridbagDemo extends JPanel/* 定 义记忆面板按 钮 */ private JButton MC, MR, MS, Mjia, Mjian;/* 定 义数字面板按 钮 */private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bpo, ba, bs, bp, bd, be;/* 定 义其他按 钮 */private JButton square_2x, mod, one_div_x;private JButton backspace, CE, C, netive;/

8、* 布局使用 */public int anchor = GridBagConstraints.CENTER;/ 布局使用public int fill = GridBagConstraints.BOTH;/ 布局使用public Insets insets = new Insets(0, 0, 0, 0);/ 布局使用public GridbagDemo()/*跟其他一 样, 这样设置*/setLayout(new GridBagLayout();initButton();addButton();private void initButton()/*按钮初始化,不解释*/b0 = new J

9、Button(0);b1 = new JButton(1);b2 = new JButton(2);b3 = new JButton(3);b4 = new JButton(4);b5 = new JButton(5);b6 = new JButton(6);b7 = new JButton(7);b8 = new JButton(8);b9 = new JButton(9);bpo = new JButton(.);ba = new JButton() ;bs = new JButton();bp = new JButton(*);bd = new JButton(/);be = new J

10、Button();/* 创建 记忆按钮 */MC = new JButton(MC);MR = new JButton(MR);MS = new JButton(MS);Mjia = new JButton(M+);Mjian = new JButton(M-);/* 其他按 钮 */backspace = new JButton();CE = new JButton(CE);C = new JButton(C);netive = new JButton();square_2x = new JButton();mod = new JButton(%);one_div_x = new JButt

11、on(1/x);private void addButton()/* 构建图形界面,关键部分 */add(new JTextField(), new GridBagConstraints(0, 0, 5, 1, 100, 30, anchor, fill, insets, 0, 0);add(MC, new GridBagConstraints(0, 1, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(MR, new GridBagConstraints(1, 1, 1, 1, 20, 10, anchor, fill, insets,0, 0);

12、add(MS, new GridBagConstraints(2, 1, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(Mjia, new GridBagConstraints(3, 1, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(Mjian, new GridBagConstraints(4, 1, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(backspace, new GridBagConstraints(0, 2, 1, 1, 20, 10,

13、anchor, fill, insets, 0, 0);add(CE, new GridBagConstraints(1, 2, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(C, new GridBagConstraints(2, 2, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(netive, new GridBagConstraints(3, 2, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(square_2x, new GridBagConstr

14、aints(4, 2, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b7, new GridBagConstraints(0, 3, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b8, new GridBagConstraints(1, 3, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b9, new GridBagConstraints(2, 3, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(bd, n

15、ew GridBagConstraints(3, 3, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(mod, new GridBagConstraints(4, 3, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b4, new GridBagConstraints(0, 4, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b5, new GridBagConstraints(1, 4, 1, 1, 20, 10, anchor, fill, insets

16、, 0, 0);add(b6, new GridBagConstraints(2, 4, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(bp, new GridBagConstraints(3, 4, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(one_div_x, new GridBagConstraints(4, 4, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b1, new GridBagConstraints(0, 5, 1, 1, 20, 10, anchor, fill, insets, 0, 0);add(b2, new GridBagConstraints(1, 5,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号