关于GridBagLayout的讲解哦

13-08-29 17:01:10|  分类: Java |  标签:gridbaglayout  gridbagconstraints  添加方法  |字号 订阅

 
 
GridBagLayout:网格包布局管理器
 
GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说:
GridBagConstraints gridBagConstraints=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,insets,ipadx,ipady);
 
具体的参数含义如下:
 
gridx,gridy:
用来设置组件的相对位置。
 
gridwidth,gridheight:
用来设置组件所占的单位长度与高度,默认值皆为1。
 
weightx,weighty:
用来设置窗口变大时各组件跟随变大的比例,当数字越大其表示的含义是组件能够得到更大的空间,默认值皆为0。
 
anchor:
当组件空间大于组件本身时,要将组建置于何处。
 
insets:
设置组件之间彼此的间距,它有四个参数,分别是上、左、下、右,默认值皆为0.
 
ipadx,ipady:
设置组件间距,默认值皆为0。
 
本文以在JPanel面板中添加三个JButton按钮为例来进行进一步的说明。

源代码:
package TestTwo;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest extends JFrame{
public GridBagLayoutTest(String title){
super(title);
initUI();
}
private void initUI(){
this.setBounds(100,100,350,200);
this.setVisible(true);
this.getContentPane().add(getPanelThree());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel getPanelOne(){
JPanel jpl1=new JPanel();
jpl1.setLayout(new GridBagLayout());
GridBagConstraints gb1=new GridBagConstraints();
JButton jb1=new JButton("按钮1");
gb1.insets=new Insets(15,15,15,15);
JButton jb2=new JButton("按钮2");
gb1.gridx=0;
gb1.gridy=0;
gb1.gridwidth=2;
gb1.gridheight=1;
gb1.fill=GridBagConstraints.BOTH;
jpl1.add(jb1,gb1);
gb1.gridx=0;
gb1.gridy=1;
jpl1.add(jb2,gb1);
jpl1.setBackground(Color.WHITE);
return jpl1;
}
private JPanel getPanelTwo(){
JPanel jpl2=new JPanel();
jpl2.setLayout(new GridBagLayout());
GridBagConstraints gb2=new GridBagConstraints();
JButton jb3=new JButton("按钮3");
gb2.gridx=0;
gb2.gridy=0;
gb2.gridwidth=2;
gb2.gridheight=2;
gb2.fill=GridBagConstraints.BOTH;
jpl2.add(jb3,gb2);
jpl2.setBackground(Color.WHITE);
return jpl2;
}
private JPanel getPanelThree(){
JPanel jpl3=new JPanel();
jpl3.setLayout(new GridBagLayout());
GridBagConstraints gb3=new GridBagConstraints();
gb3.gridx=0;
gb3.gridy=0;
gb3.weightx=2.0;
gb3.weighty=1.0;
gb3.fill=GridBagConstraints.BOTH;
jpl3.add(getPanelOne(),gb3);
gb3.gridx=1;
gb3.gridy=0;
gb3.weightx=3.0;
jpl3.add(getPanelTwo(),gb3);
return jpl3;
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new GridBagLayoutTest("网格包布局管理器"); 
}
}
运行的结果:
 
 
方法二:
package TestTwo;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTestTwo extends JFrame{
private JButton jb1,jb2,jb3;
public GridBagLayoutTestTwo(String title){
super(title);
initUI();
}
private void initUI(){
this.setBounds(100,100,350,200);
this.setVisible(true);
this.getContentPane().add(getPanelThree());
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
private JPanel getPanelThree(){
JPanel jpl3=new JPanel();
jpl3.setLayout(new GridBagLayout());
GridBagConstraints gb3=new GridBagConstraints();
gb3.gridx=0;
gb3.gridy=0;
gb3.weightx=2.0;
gb3.weighty=1.0;
gb3.fill=GridBagConstraints.BOTH;
jpl3.add(getPanelOne(),gb3);
gb3.gridx=1;
gb3.gridy=0;
gb3.weightx=3.0;
jpl3.add(getPanelTwo(),gb3);
return jpl3;
}
private JPanel getPanelOne(){
JPanel jpl1=new JPanel();
GridBagLayout gridBagLayout=new GridBagLayout();
jpl1.setLayout(gridBagLayout);
gridBagLayout.setConstraints(getJButtonOne(),new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(35,35,15,65),0,0));
jpl1.add(getJButtonOne());
gridBagLayout.setConstraints(getJButtonTwo(),new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(15,35,35,65),0,0));
jpl1.add(getJButtonTwo());
jpl1.setBackground(Color.WHITE);
return jpl1;
}
private JButton getJButtonOne(){
if(jb1==null){
jb1=new JButton("按钮1");
}
return jb1;
}
private JButton getJButtonTwo(){
if(jb2==null){
jb2=new JButton("按钮2");
}
return jb2;
}
private JPanel getPanelTwo(){
JPanel jpl2=new JPanel();
GridBagLayout gridBagLayout=new GridBagLayout();
jpl2.setLayout(gridBagLayout);
gridBagLayout.setConstraints(getJButtonThree(),new GridBagConstraints(0,0,2,2,0,0,GridBagConstraints.SOUTH,GridBagConstraints.BOTH,new Insets(15,15,15,35),0,0));
jpl2.add(getJButtonThree());
jpl2.setBackground(Color.WHITE);
return jpl2;
}
private JButton getJButtonThree(){
if(jb3==null){
jb3=new JButton("按钮3");
}
return jb3;
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new GridBagLayoutTestTwo("网格包布局管理器二");
}
}
运行的结果:
 
 

你可能感兴趣的:(GridBagLayout)