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