java 图形化布局_Java图形化界面设计——布局管理器之GridBagLayout

GridBagLayout 不会随着窗口的变化标签随之发生变化,可固定。

----------------------------------------------------------

import java.awt.Button;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class GridBagEx2 extends  JPanel

{

private static final long serialVersionUID = -5214441555967215113L;

protected void makebutton(String name, GridBagLayout gridbag,

GridBagConstraints c)

{

Button button = new Button(name);

gridbag.setConstraints(button, c);

add(button);

}

public void init()

{

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();

setFont(new Font("SansSerif", Font.PLAIN, 14));

setLayout(gridbag);

c.fill = GridBagConstraints.BOTH;

c.weightx = 1.0;

makebutton("Button1", gridbag, c);

makebutton("Button2", gridbag, c);

makebutton("Button3", gridbag, c);

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);

setSize(300, 100);

}

public static void main(String args[])

{

JFrame f = new JFrame("GridBag Layout Example");

f.setLocation(400, 200);

GridBagEx2 ex1 = new GridBagEx2();

ex1.init();

f.add("Center", ex1);

f.pack();

f.setSize(f.getPreferredSize());

f.setVisible(true);

f.addWindowListener(new WindowAdapter()

{

@Override

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

原文:http://www.cnblogs.com/aipan/p/6410388.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值