GridBagLayout参数说明

GridBagLayout参数说明
GridBagLayout是java swing中一种版面管理器,很强大、也比较难使用。
GridBagLayout只是一种构造函数,但GridBagLayout必须配合GridBagConstaints才能达到设置的效果。


构造函数:
GridBagLayout(); 建立一个信的GridBagLayout管理器
GridBagConstaints(); 建立一个信的GridBagConstaints对象
GridBagConstaints(int gridx, int gridy, int gridwidth, int gridheight, dobule weightx, dobule weighty, int anchor,int fill, Insets insets, int ipadx, int ipady)
建立一个信的GridBagConstaints对象,并指定其参数值

public class GridBagLayoutDemo{
	public GridBagLayoutDemo(){
		JButton b;
		GridBagConstaints c;
		int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
		dobule weightx,weighty;
		Insets inset;
		
		JFrame f = new JFrame();
		
		GridBagLayout gridbag = new GridBagLayout();
		Container contentPane = f.getContentPane();
		contentPane.setLayout(gridbag);
		
		b = new JButton("first");
		gridx = 0;//定位行
		gridy = 0;//定位列
		gridwidth = 1;//设置组件所占的单位长度
		gridheight= 1;//设置组件所占的单位高度
		weightx = 10;//设置当窗口大小改变时,组件宽度变化的比例。数字越大,表示组件能得到更多空间。默认为0
		weighty = 1;//设置当窗口大小改变时,组件高度变化的比例。数字越大,表示组件能得到更多空间。默认为0
		anchor = GridBagConstraints.CENTER;//当组件空间大于组件本身时,组件所处位置。(CENTER,NORTH,NORTHEAST,EAST,SOUTHEAST,SOUTH,SOUTHWEST,WEST,NORTHWEST)
		fill = GridBagConstraints.HORIZONTAL;//当组件所在位置有剩余空间时,设置其填充方式(NONE,VERTICAL,HORIZONTAL,BOTH)
		inset = new Insets(0,0,0,0);//设置组件之间的间距,四个参数表别表示:上、左、下、右
		ipadx = 0;//设置组件内的水平间距,默认为0
		ipady = 0;//设置组件内的垂直间距,默认为0
		c = new GridBagConstaints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(b,c);
		contentPane.add(b);
		
		f.pack();
		f.setVisible(true);
	}
	
	public static void main(String[] args){
		new GridBagLayoutDemo();
	}
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值