GridBagLayoutAPI例子测试

 

package lab9;

import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
/**
 * 
 * @Project_name: JavaLesson
 * @Description: 测试GridBagLayout
 * @Author LinYiSong
 * @Date: 2010-11-21~2010-11-21
 * @Vesion:
 */
public class GridBagLayoutTest {

	private JFrame app = new JFrame();

	/**
	 * 在网格布局中GridBagLayout按约束GridBagConstraints生成对应的Button
	 * @param name
	 * @param gridbag
	 * @param c
	 */
	public void makebutton(String name, GridBagLayout gridbag,
			GridBagConstraints c) {
		Button button = new Button(name);
		gridbag.setConstraints(button, c);
		app.add(button);
	}
	
	public void launchFrame() {
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setLocation(100, 300);
		app.setSize(200, 200);

		/*
		 *网格布局 
		 */
		GridBagLayout gridbag = new GridBagLayout();
		/*
		 * GridBagConstraints 类指定使用 GridBagLayout 类布置的组件的约束
		 */
		GridBagConstraints c = new GridBagConstraints();
		app.setLayout(gridbag);

		/**
		 * fill
		 * 当组件的显示区域(框体大小)大于它所请求的显示区域的大小(原本设定的大小)时使用此字段。(放大框体)
		 * 它可以确定是否调整组件大小,以及在需要的时候如何进行调整。 
		 * 以下值适用于 fill: 
		 * NONE:不调整组件大小。 
         * HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度。 
         * VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。 
	     * BOTH:使组件完全填满其显示区域。
		 */
		c.fill = GridBagConstraints.BOTH;
		/**
		 * weightx
		 * 指定如何分布额外的水平空间,其中额外是指 水平空间上 布局< 需填充(还有更多空间允许填充)
		 * 系统会将额外的空间按照其权重比例分布到每一列,而列的权重将是列的所有组件中最大的 weightx
		 * 权重为零的列不会得到额外的空间。 如果所有的权重都为零,则所有的额外空间都将出现在单元格的网格之间和左右边缘之间
		 */
		c.weightx = 1.0;
		makebutton("Button1", gridbag, c);
		makebutton("Button2", gridbag, c);
		makebutton("Button3", gridbag, c);

		/**
		 * gridwidth
		 *指定组件显示区域的  某一行  的单元格数 
		 *REMAINDER 是指除了剩下的未指定的显示区域
		 *RELATIVE  从gridx~倒2格
		 *gridwidth 应为非负,默认值为 1。
		 */
		c.gridwidth = GridBagConstraints.REMAINDER; // end row
		makebutton("Button4", gridbag, c);

		c.weightx = 0.0; // reset to the default
		makebutton("Button5", gridbag, c); // another row

		c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last in row
		makebutton("Button6", gridbag, c);

		c.gridwidth = GridBagConstraints.REMAINDER; // end row
		makebutton("Button7", gridbag, c);

		c.gridwidth = 1; // reset to the default
		c.gridheight = 2;
		c.weighty = 1.0;
		makebutton("Button8", gridbag, c);

		c.weighty = 0.0; // reset to the default
		c.gridwidth = GridBagConstraints.REMAINDER; // end row
		c.gridheight = 1; // reset to the default
		makebutton("Button9", gridbag, c);
		makebutton("Button10", gridbag, c);
		
		app.setVisible(true);

	}

	

	public GridBagLayoutTest() {
		launchFrame();
		app.setVisible(true);
	}

	public static void main(String[] args) {
		new GridBagLayoutTest();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值