GridBagLayout:网格包布局管理器
来源:互联网 发布:淘宝商品制作 编辑:程序博客网 时间:2024/05/17 03:43
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。
源代码:
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("网格包布局管理器二");
}
}
运行的结果:
0 0
- GridBagLayout:网格包布局管理器
- GridBagLayout网格包布局管理器
- java GridBagLayout 网格包布局管理器
- swing 网格布局管理器GridBagLayout
- java的GridBagLayout网格包布局管理器使用详解
- Java布局管理器(四)之GridBagLayout(网格包布局管理)
- Swing无序网格布局管理器(GridBagLayout)应用详解
- 网格布局 GridBagLayout()
- swing 布局管理器 GridBagLayout
- GridBagLayout布局管理器
- GridBagLayout布局管理器
- Java布局管理器GridBagLayout布局
- JAVA之网格布局管理器中JSeparator的使用以及GridBagLayout的注意细节
- java 布局管理器 gridbaglayout使用
- GridBagLayout布局管理器应用详解
- GridBagLayout布局管理器的应用
- GridBagLayout布局管理器应用详解
- GridBagLayout布局管理器应用详解
- 枚举,反射学习及其总结
- C#——可变参数示例
- 共享几个项目中用到的MySQL扩展函数
- 类模板
- 黑马程序员_Java GUI
- GridBagLayout:网格包布局管理器
- 关于Mybaits映射文件<select>的条件拼接方法
- vs 2008 自定义Settimer时间
- 抛弃jQuery 深入原生的JavaScript
- 黑马程序员_关于循环引用
- 双向链表
- Android关于gridview平滑滚动定位smoothScrollToPositionFromTop方法测试
- Java中GridBagLayout的使用
- 文档类APP 文件家 免费PDF批注