Java的GridLayout布局管理器:
来源:互联网 发布:网球比赛直播软件 编辑:程序博客网 时间:2024/05/16 10:05
GridLayout布局管理器:
(1)GridLayout布局管理器将容器分割为纵横线分割的网格,每个网格所占的大小相同。当使用其向布局管理器中添加组件时,默认从左向右,从上至下依次添加到每个网格中,、。
(2)GridLayout有两个构造器
GridLayout(int rows ,int cols) 采用指定的行数,列数以及默认的横向间距、纵向间距将容器分割为多个网格
GridLayout(int rows ,int cols,int hgap,int vgap) 采用指定的行数,列数以及指定的横向间距、纵向间距将容器分割为
多个网格
1. 创建GridBagLayout,并指定容器使用该布局管理器
1. GridBagLayout gb = new GridBagLayout(); 2. container.setLayout(gb);
创建GridBagConstraints的对象,并设置该对象的相关属性
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridx=2;
- gbc.gridy=1;
- gbc.gridwidth=2;
- gbc.gridheight=1;
调用GridBagLayout对象的方法来建立GridBagConstraints对象与受控制组件之间的联系。
- gb.setConstraints(c,gbc);//设置c组件受gbc控制
添加组件
- container.add(c);
通常我们可以将2,3,4步写成一个addComponent方法,为这个方法传递所需要的参数,来进行添加组件的化简。例如:
public void addComponent(Component c, int gx, int gy, int gw,int gh)
{
this.gridx=gx;
this.gridy=gy;
this.gridwidth=gw;
this.gridheight=gh;
gb.setConstraints(c,gbc); container.add(c); }
使用GridBagLayout关键在于GridBagConstraints,该类具有如下几个方法:
fill:设置组件如何占领空白区域,它可取如下几个值:GridBagConstraints.NONE, GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL, GridBagConstraints.BOTH。
gridx,gridy:设置组件的左上角所在网格的索引(网格的索引从0 开始),此外这两个值还可以设为GridBagConstraints.RELATIVE,这个值也是默认值,它表明当前组件紧跟在上一个组件之后。
gridwidht和gridheight:设置组件横向纵向跨越多少个网格,他们的默认值都是1,如果该组件是横向或纵向的最后一个还可以将此值设为GridBagConstraints.REMAINDER,若为倒数第二个组件则可以设值为GridBagConstraints.RELATIVE。
ipadx和ipady:设置组件横向纵向的内部填充大小,即在组件的最小尺寸上还需要增大多少,若设置了这个值则组件在最小尺寸的基础上增大ipadx*2或 ipady*2像素。
weightx和weighty(double类型):就是权重,也就是组件组件占领多余空间的水平或垂直增加比例,默认值为0也就是不占领多余空间。例如有三个组件,我们将他们的水平增加比例分别设为1.0,2.0,3.0,当容器宽度增加60像素时,他们分别增加10,20和30像素。如果我们希望某个组件的大小会随着容器的变化而变化,我们需要同时设置fill和weightx,weighty属性。
Swing中的BoxLayout布局管理器提供了一个构造器:BoxLayout(Container targer,int axis),它制定创建基于targer容器的BoxLayout布局管理器,它里面的组件按axis方向排列,axis有BoxLayout.X_AXIS和BoxLayout.Y_AXIS两个方向。BoxLayout通常和Box容器结合使用,Box容器有点像Panel,它默认使用BoxLayout布局管理器。Box有两个静态方法来创建Box对象:createHorizontalBox()和createVerticalBox(),一旦获得了Box容器之后,就可以使用Box来承装普通GUI组件,然后再将这些Box组件添加到其他容器中,从而形成整体的窗口布局。例如:
1. public class Test 2. { 3. private Frame f = new Frame("cs"); 4. private Box horizontal = Box.createHorizontalBox(); 5. private Box vertical = Box.createVerticalBox(); 6. public void init() 7. { 8. horizontal.add(new Button("shuiping1")); 9. horizontal.add(new Button("shuiping2")); 10. vertical.add(new Button("chuizhi1")); 11. vertical.add(new Button("chuizhi2")); 12. f.add(horizontal, BorderLayout.NORTH); 13. f.add(vertical); 14. f.pack(); 15. f.setVisible(true); 16. } 17. public static void main(String[] args) 18. { 19. new Test().init(); 20. } 21. }
最后我们还可以使用绝对定位:只需要将Container的布局管理器设为null即可,也就是setLayout(null),往容器中加组件的时候指定组件的位置和大小。一般调用setBounds(int x,int y,int width,int height)。
- Java的GridLayout布局管理器:
- Java的GridLayout布局管理器:
- [Java] GridLayout 布局管理器
- Java.布局管理器.CardLayout-GridLayout
- 有关GridLayout布局管理器
- GridLayout布局管理器
- 网格布局管理器GridLayout
- GridLayout布局管理器
- GridLayout布局管理器
- Java Swing界面编程(10)---GridLayout布局管理器
- Java AWT编程 布局管理器(三)GridLayout
- Java FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout布局管理器
- Java FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout布局管理器
- Java FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout布局管理器
- 布局管理器BorderLayout、FlowLayout、GridLayout
- 11.3布局管理器补充 GridLayout
- GridLayout(表格布局管理器)
- GUI GridLayout布局管理器 Frame的pack方法
- ReactRouter 4 前瞻
- lazarus:用分隔符分割字符串
- opencv的Sobel导数、Scharr滤波器、Laplacian算子、Canny边缘检测
- 数据库的隔离级别与并发问题
- php借助mcript扩展实现对称加密
- Java的GridLayout布局管理器:
- Java的CardLayout布局管理器:
- php使用openssl来实现非对称加密
- Java的BoxLayout布局管理器:
- AWT常用控件:
- c++中的const为真正意义上的const
- stlink usb communication error情况
- Linux下的静态库、动态库和动态加载库
- "计算机意外地重新启动或遇到错误。windows安装无法继续"怎么办