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 [Leetcode 137]Single Number II

    题目描述: Given an array of integers, every element appears three times except for one. Find that single ...

  2. Servlet,JDBC,JSONObject三者配和处理客户端请求并返回正确的json数据

    JSON简介 首先我们来理解json(JavaScript Object Notation),如果你熟悉python的字典结构和列表结构,其实json格式是非常容易理解的,当然不熟也不难理解,网上的资 ...

  3. 【转】./a.out 2>&1 > outfile

    原文网址:http://www.cnblogs.com/zhaoyl/archive/2012/10/22/2733418.html APUE 3.5关于重定向有个容易迷惑人的问题: ./a.out ...

  4. Asp.Net MVC4 系列-- 进阶篇之路由(1)【转】

    http://blog.csdn.net/lan_liang/article/details/22993839?utm_source=tuicool

  5. Oracle 课程八之性能优化之10053事件

    一. 10053事件 当一个SQL出现性能问题的时候,可以使用SQL_TRACE 或者 10046事件来跟踪SQL. 通过生成的trace来了解SQL的执行过程. 我们在查看一条SQL的执行计划的时候 ...

  6. HDU 5734 Acperience

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. C#中的值类型(value type)与引用类型(reference type)的区别

    ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部     C#中 ...

  8. 程序破解之 API HOOK技术 z

    API HOOK,就是截获API调用的技术,在程序对一个API调用之前先执行你的函数,然后根据你的需要可以执行缺省的API调用或者进行其他处理,假设如果想截获一个进程对网络的访问,一般是几个socke ...

  9. [OFBiz]开发 二

    1.svn中check出的apache-ofbiz-10.04(svn_2010-04-01代码备分)由于它的所有文件都不含有中文,所以Eclipse使用什么编码方式都可以(ISO, GBK, UTF ...

  10. 【C++对象模型】构造函数语意学之一 默认构造函数

    默认构造函数,如果程序员没有为类定义构造函数,那么编译器会在[需要的时候]为类合成一个构造函数,而[需要的时候]分为程序员需要的时候和编译器需要的时候,程序员需要的时候应该由程序员来做工作,编译器需要 ...