gridbagconstraints

转载 2016年06月01日 11:38:45
package com.wanju.chat.test;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ExampleFrame_03 extends JFrame {

	public static void main(String args[]) {
		ExampleFrame_03 frame = new ExampleFrame_03();
		frame.setVisible(true);
	}

	public ExampleFrame_03() {
		super();
		setTitle("使用网格组布局管理器");
		getContentPane().setLayout(new GridBagLayout());
		setBounds(100, 100, 500, 170);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JButton button = new JButton("A");
		final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridy = 0;// 起始点为第1行
		gridBagConstraints.gridx = 0;// 起始点为第1列
		gridBagConstraints.weightx = 10;// 第一列的分布方式为10%
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(button, gridBagConstraints);

		final JButton button_1 = new JButton("B");
		final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
		gridBagConstraints_1.gridy = 0;
		gridBagConstraints_1.gridx = 1;
		gridBagConstraints_1.insets = new Insets(0, 5, 0, 0);// 设置组件左侧的最小距离
		gridBagConstraints_1.weightx = 20;// 第一列的分布方式为20%
		gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(button_1, gridBagConstraints_1);

		final JButton button_2 = new JButton("C");
		final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
		gridBagConstraints_2.gridy = 0;// 起始点为第1行
		gridBagConstraints_2.gridx = 2;// 起始点为第3列
		gridBagConstraints_2.gridheight = 2;// 组件占用两行
		gridBagConstraints_2.insets = new Insets(0, 5, 0, 0);
		gridBagConstraints_2.weightx = 30;// 第一列的分布方式为30%
		gridBagConstraints_2.fill = GridBagConstraints.BOTH;// 同时调整组件的宽度和高度
		getContentPane().add(button_2, gridBagConstraints_2);

		final JButton button_3 = new JButton("D");
		final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
		gridBagConstraints_3.gridy = 0;
		gridBagConstraints_3.gridx = 3;
		gridBagConstraints_3.gridheight = 4;
		gridBagConstraints_3.insets = new Insets(0, 5, 0, 5);// 设置组件左侧和右侧的最小距离
		gridBagConstraints_3.weightx = 40;// 第一列的分布方式为40%
		gridBagConstraints_3.fill = GridBagConstraints.BOTH;
		getContentPane().add(button_3, gridBagConstraints_3);

		final JButton button_4 = new JButton("E");
		final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
		gridBagConstraints_4.gridy = 1;
		gridBagConstraints_4.gridx = 0;
		gridBagConstraints_4.gridwidth = 2;// 组件占用两列
		gridBagConstraints_4.insets = new Insets(5, 0, 0, 0);// 设置组件上方的最小距离
		gridBagConstraints_4.fill = GridBagConstraints.HORIZONTAL;// 只调整组件的宽度
		getContentPane().add(button_4, gridBagConstraints_4);

		final JButton button_5 = new JButton("F");
		final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
		gridBagConstraints_5.gridy = 2;// 起始点为第3行
		gridBagConstraints_5.gridx = 0;// 起始点为第1列
		gridBagConstraints_5.insets = new Insets(5, 0, 0, 0);
		gridBagConstraints_5.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(button_5, gridBagConstraints_5);

		final JButton button_6 = new JButton("G");
		final GridBagConstraints gridBagConstraints_6 = new GridBagConstraints();
		gridBagConstraints_6.gridy = 2;
		gridBagConstraints_6.gridx = 1;
		gridBagConstraints_6.gridwidth = 2;// 组件占用两列
		gridBagConstraints_6.gridheight = 2;// 组件占用两行
		gridBagConstraints_6.insets = new Insets(5, 5, 0, 0);
		gridBagConstraints_6.fill = GridBagConstraints.BOTH;// 只调整组件的高度
//		gridBagConstraints_6.fill = GridBagConstraints.VERTICAL;// 只调整组件的高度
//		gridBagConstraints_6.ipadx = 30;// 增加组件的首选宽度
//		gridBagConstraints_6.anchor = GridBagConstraints.EAST;// 显示在东方
		getContentPane().add(button_6, gridBagConstraints_6);

		final JButton button_7 = new JButton("H");
		final GridBagConstraints gridBagConstraints_7 = new GridBagConstraints();
		gridBagConstraints_7.gridy = 3;
		gridBagConstraints_7.gridx = 0;
		gridBagConstraints_7.insets = new Insets(5, 0, 0, 0);
		gridBagConstraints_7.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(button_7, gridBagConstraints_7);
		//
	}

}

相关文章推荐

GridBagConstraints

/* GBC - A convenience class to tame the GridBagLayout Copyright (C) 2002 Cay S. Horstmann (http://...

GridBagConstraints(网格组布局管理器的使用)

GridBagConstraints特征: 由GridBagConstraints类实现的布局管理器称为网格组布局管理器,它实现了一个动态的矩形网格,这个矩形风格由无数个矩形单元格组成,每个组件可...

GridBagLayout 以及 GridBagConstraints 用法

GridBagLayout是一个灵活的布局管理器,部件如果想加入其中需借助GridBagConstraints,其中有若干个参数,解释如下: gridx/gridy:组件的横纵坐标 gri...

java GridBagConstraints解析

说明:GridBagLayout只有一个无参的构造器,要使用它就必须用setConstraints(Component comp,GridBagConstraints constraints)将它和G...

GridBagConstraints gbc_browserButton1

GridBagConstraints gbc_browserButton1 = new GridBagConstraints();   gbc_browserButton1.insets = new...

JAVA布局模式:GridBagConstraints终极技巧参数详解

布局模式 :GridBagConstraints布局,先发一个实例: gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 g...

JAVA布局模式:GridBagConstraints终极技巧

JAVA布局模式:GridBagConstraints终极技巧  (2006-11-14 21:07:33) 转载▼     最近正在修改《公...
  • ly4983
  • ly4983
  • 2015年04月10日 09:59
  • 398

JAVA布局模式:GridBagConstraints

布局模式 :GridBagConstraints布局,先发一个实例: gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 g...
  • zjhwqx
  • zjhwqx
  • 2017年06月13日 13:50
  • 65

GridBagConstraints参数详解

GridBagConstraints参数详解gridBagConstraints 参数 gridx = 2; // X=2 gridy = 0; // Y=0 gridwidth = 1; /...
  • zxlqaz
  • zxlqaz
  • 2017年03月29日 10:47
  • 886

GridBagConstraints布局

查看API文档,我们就知道GridBagConstraints有十一个属性!自我感觉API文档里面有些东西讲得不是很好理解,就象gridx 与 gridy 这两个属性一样,有些书上说gridx表示行,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:gridbagconstraints
举报原因:
原因补充:

(最多只允许输入30个字)