13-08-29 17:01:10|  分类: Java |  标签:gridbaglayout  gridbagconstraints  添加方法  |字号 订阅

 
 
GridBagLayout:网格包布局管理器
 
GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说:
GridBagConstraints gridBagConstraints=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,insets,ipadx,ipady);
 
具体的参数含义如下:
 
gridx,gridy:
用来设置组件的相对位置。
 
gridwidth,gridheight:
用来设置组件所占的单位长度与高度,默认值皆为1。
 
weightx,weighty:
用来设置窗口变大时各组件跟随变大的比例,当数字越大其表示的含义是组件能够得到更大的空间,默认值皆为0。
 
anchor:
当组件空间大于组件本身时,要将组建置于何处。
 
insets:
设置组件之间彼此的间距,它有四个参数,分别是上、左、下、右,默认值皆为0.
 
ipadx,ipady:
设置组件间距,默认值皆为0。
 

本文以在JPanel面板中添加三个JButton按钮为例来进行进一步的说明。

源代码:
package TestTwo;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest extends JFrame{
public GridBagLayoutTest(String title){
super(title);
initUI();
}
private void initUI(){
this.setBounds(100,100,350,200);
this.setVisible(true);
this.getContentPane().add(getPanelThree());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel getPanelOne(){
JPanel jpl1=new JPanel();
jpl1.setLayout(new GridBagLayout());
GridBagConstraints gb1=new GridBagConstraints();
JButton jb1=new JButton("按钮1");
gb1.insets=new Insets(15,15,15,15);
JButton jb2=new JButton("按钮2");
gb1.gridx=0;
gb1.gridy=0;
gb1.gridwidth=2;
gb1.gridheight=1;
gb1.fill=GridBagConstraints.BOTH;
jpl1.add(jb1,gb1);
gb1.gridx=0;
gb1.gridy=1;
jpl1.add(jb2,gb1);
jpl1.setBackground(Color.WHITE);
return jpl1;
}
private JPanel getPanelTwo(){
JPanel jpl2=new JPanel();
jpl2.setLayout(new GridBagLayout());
GridBagConstraints gb2=new GridBagConstraints();
JButton jb3=new JButton("按钮3");
gb2.gridx=0;
gb2.gridy=0;
gb2.gridwidth=2;
gb2.gridheight=2;
gb2.fill=GridBagConstraints.BOTH;
jpl2.add(jb3,gb2);
jpl2.setBackground(Color.WHITE);
return jpl2;
}
private JPanel getPanelThree(){
JPanel jpl3=new JPanel();
jpl3.setLayout(new GridBagLayout());
GridBagConstraints gb3=new GridBagConstraints();
gb3.gridx=0;
gb3.gridy=0;
gb3.weightx=2.0;
gb3.weighty=1.0;
gb3.fill=GridBagConstraints.BOTH;
jpl3.add(getPanelOne(),gb3);
gb3.gridx=1;
gb3.gridy=0;
gb3.weightx=3.0;
jpl3.add(getPanelTwo(),gb3);
return jpl3;
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new GridBagLayoutTest("网格包布局管理器"); 
}
}
运行的结果:
 
方法二:
package TestTwo;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTestTwo extends JFrame{
private JButton jb1,jb2,jb3;
public GridBagLayoutTestTwo(String title){
super(title);
initUI();
}
private void initUI(){
this.setBounds(100,100,350,200);
this.setVisible(true);
this.getContentPane().add(getPanelThree());
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
private JPanel getPanelThree(){
JPanel jpl3=new JPanel();
jpl3.setLayout(new GridBagLayout());
GridBagConstraints gb3=new GridBagConstraints();
gb3.gridx=0;
gb3.gridy=0;
gb3.weightx=2.0;
gb3.weighty=1.0;
gb3.fill=GridBagConstraints.BOTH;
jpl3.add(getPanelOne(),gb3);
gb3.gridx=1;
gb3.gridy=0;
gb3.weightx=3.0;
jpl3.add(getPanelTwo(),gb3);
return jpl3;
}
private JPanel getPanelOne(){
JPanel jpl1=new JPanel();
GridBagLayout gridBagLayout=new GridBagLayout();
jpl1.setLayout(gridBagLayout);
gridBagLayout.setConstraints(getJButtonOne(),new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(35,35,15,65),0,0));
jpl1.add(getJButtonOne());
gridBagLayout.setConstraints(getJButtonTwo(),new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(15,35,35,65),0,0));
jpl1.add(getJButtonTwo());
jpl1.setBackground(Color.WHITE);
return jpl1;
}
private JButton getJButtonOne(){
if(jb1==null){
jb1=new JButton("按钮1");
}
return jb1;
}
private JButton getJButtonTwo(){
if(jb2==null){
jb2=new JButton("按钮2");
}
return jb2;
}
private JPanel getPanelTwo(){
JPanel jpl2=new JPanel();
GridBagLayout gridBagLayout=new GridBagLayout();
jpl2.setLayout(gridBagLayout);
gridBagLayout.setConstraints(getJButtonThree(),new GridBagConstraints(0,0,2,2,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(15,15,15,35),0,0));
jpl2.add(getJButtonThree());
jpl2.setBackground(Color.WHITE);
return jpl2;
}
private JButton getJButtonThree(){
if(jb3==null){
jb3=new JButton("按钮3");
}
return jb3;
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new GridBagLayoutTestTwo("网格包布局管理器二");
}
}
运行的结果:
 

关于GridBagLayout的讲解哦的更多相关文章

  1. java经典5种 FlowLayout 、BorderLayout、GridLayout、GridBagLayout、CardLayout布局

    Java 程序通过jvm可以很好的移植到其他平台上,但是java 生成的图形界面样式,在不使用布局的情况下,往往需要重新设定大小,才能在新的平台上调整到最佳样式.这是由于组件的最佳大小 往往是与平台相 ...

  2. PHP与API讲解(一)

    了解API: 在使用与创建自己的API之前我们需要先了解什么是API! API代表应用程序编程接口,而接口指的是一个特定的服务.一个应用程序或者其他程序的公共模块. 理解SOA(面向服务的架构):SO ...

  3. 微信小程序(微信应用号)组件讲解

    这篇文章主要讲解微信小程序的组件. 首先,讲解新建项目.现在有句话:招聘三天以上微信小程序开发,这个估计只能去挖微信的工程师了.技术新,既然讲解,那我们就从开始建项目讲解. 打开微信web开发者工具, ...

  4. 免费公开课,讲解强大的文档集成组件Aspose,现在可报名

    课程①:Aspose.Total公开课内容:讲解全能型文档管理工具Aspose.Total主要功能及应用领域时间:2016-11-24 14:30 (暂定)报名地址:http://training.e ...

  5. EventBus总线讲解

    在我们公司经常用到总线,具体的总线是什么让我理解我也不清楚,但是在这几个月下来,我已经知道总线如何使用,现在加上示例讲解总线如何使用. 1. 首先我们的新建一个类,这个类其实是用于总线传递的模型 us ...

  6. FTP的搭建与虚拟目录作用<之简单讲解>

    操作系统:win7 VS2010编写WebService与在IIS的发布<之简单讲解>中我已经说了IIS安装与使用,不明白的可以跳过去看. 1.添加FTP站点 2. 3. 4. 5. zq ...

  7. Restful 介绍及SpringMVC+restful 实例讲解

    restful不是一个框架,称为一种编码更烦更贴切吧,其核心类位于spring-web.jar中,即RestTemplate.class restful是rpc通过http协议的一种实现方式,和web ...

  8. 实例讲解react+react-router+redux

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  9. 【Spring】SpringMVC入门示例讲解

    目录结构: // contents structure [-] SpringMVC是什么 Spring MVC的设计原理 SpringMVC入门示例 1,复制Jar包 2,Web.xml文件 3,My ...

随机推荐

  1. java常量池

    Java的堆是一个运行时数据区,类的(对象从中分配空间.这些对象通过new.newarray. anewarray和multianewarray等指令建立,它们不需要程序代码来显式的释放.堆是由垃圾回 ...

  2. 基于ffmpeg的流媒体服务器

    OS:ubuntu 12.04ffmpeg:N-47141-g4063bb2x264:0.133.2334 a3ac64b目标:使用ffserver建立流媒体服务器使用ffmpeg对本地文件流化(x2 ...

  3. 【Unity3D】枪战游戏—弹孔设置

    以子弹为原点,发射射线,如果射线检测到障碍物,则返回射线与障碍物的碰撞点 在该点处实例化出弹孔贴图 void Update () { transform.Translate (Vector3.forw ...

  4. iOS AFNetworking的使用

    转:http://www.cnblogs.com/lookenwu/p/3927897.html AFNetworking几乎是iOS上最常用的HTTP库了,AFNetworking也确实用起来简单, ...

  5. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  6. java web 学习十三(使用session防止表单重复提交)

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  7. 【LeetCode 213】House Robber II

    This is an extension of House Robber. After robbing those houses on that street, the thief has found ...

  8. 排序算法:七大排序算法的PHP实现

    由于最近在找工作,面试中难免会遇到一些算法题,所以就用PHP把七大排序算法都实现了一遍,也当做是一种复习于沉淀. 冒泡排序 2. 选择排序 3. 插入排序 4. 快速排序 5. 希尔排序 6. 归并排 ...

  9. PHP中我经常容易混淆的三组函数

    原文:http://www.ido321.com/1252.html 一.htmlentities() 和htmlspecialchars() 1.htmlentities() 1.1  功能:把字符 ...

  10. android sensor传感器系统架构初探

    http://blog.csdn.net/qianjin0703/article/details/5942579 http://blog.chinaunix.net/uid-28621021-id-3 ...