gridbagconstraints(GridBagLayout布局)
大家好,感谢邀请,今天来为大家分享一下gridbagconstraints的问题,以及和GridBagLayout布局的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
谁能解释一下Java中GridBagLayout的使用
GridLayout的使用:
GridLayout的类层次结构图:
java.lang.Object
--java.awt.GridLayout
GridLayout比FlowLayout多了行和列的设置,也就是说你要先设置GridLayout共有几行
几列,就如同二维平面一般,然后你加
进去的组件会先填第一行的格子,然后再从第二行开始填,依此类扒,就像是一个个的
格子一般。而且GridLayout会将所填进去组
件的大小设为一样。
构造函数:GridLayout()建立一个新的GridLayout,默认值是1行1列。
GridLayout(int rows,int
cols)建立一个几行几列的GridLayout.
GridLayout(int rows,int cols, int hgap,int
vgap)建立一个几行几列的GridLayout,并设置组件的间距。
例子:GridLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ActionListener{
JPanel p1,p2,p3,p4;
int i=1;
JFrame f;
public CardLayoutDemo(){
f=new JFrame();//当做top-level组件
Container contentPane=f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));
p1=new JPanel();
Button b=new Button("Change Card");
b.addActionListener(this);//当按下"Change
Card"时,进行事件监听,将会有系统操作产生。
p1.add(b); file://处理操作在52-64行.
contentPane.add(p1);
p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JButton("first"));
p2.add(new JButton("second"));
p2.add(new JButton("third"));
p3=new JPanel();
p3.setLayout(new GridLayout(3,1));
p3.add(new JButton("fourth"));
p3.add(new JButton("fifth"));
p3.add(new JButton("This is the last button"));
p4=new JPanel();
p4.setLayout(new CardLayout());
p4.add("one",p2);
p4.add("two",p3);
/*要显示CardLayout的卡片,除了用show(Container parent,String
name)这个方法外
*,也可试试first(Container),next(Container),previous(Container),last
(Container)这
*四个方法,一样可以达到显示效果。
*/
((CardLayout)p4.getLayout()).show(p4,"one");
contentPane.add(p4);
f.setTitle("CardLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent event){
switch(i){
case 1:
((CardLayout)p4.getLayout()).show(p4,"two");
break;
case 2:
((CardLayout)p4.getLayout()).show(p4,"one");
break;
}
i++;
if(i==3) i=1;
f.validate();
}
public static void main(String[] args){
new CardLayoutDemo();
}
}
1-3-5:GridBagLayout的使用:是java中最有弹性但也是最复杂的一种版面管理器。它
只有一种构造函数,但必须配合
GridBagConstraints才能达到设置的效果。
GridBagLayout的类层次结构图:
java.lang.Object
--java.awt.GridBagLayout
构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器。
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,int
gridwidth,int gridheight,double weightx,double weighty,
int anchor,int fill, Insets
insets,int ipadx,int ipady)建立一个新的GridBagConstraints对象
,并指定其参数的值。
参数说明:
gridx,gridy:设置组件的位置,gridx设置为GridBagConstraints.RELATIVE代表此组件
位于之前所加入组件的右边。
若将gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下
面。建议定义出
gridx,gridy的位置,以便以后维护程序。表示放在几行几列,gridx=0,gridy=0时放在
0行0列。
gridwidth,gridheight:用来设置组件所占的单位长度与高度,默认值皆为1。你可以使
用GridBagConstraints.REMAINDER常
量,代表此组件为此行或此列的最后一个组件,而且会占据所
有剩余的空间。
weightx,weighty:用来设置窗口变大时,各组件跟着变大的比例,当数字越大,表示组
件能得到更多的空间,默认值皆为0。
anchor:
当组件空间大于组件本身时,要将组件置于何处,有CENTER(默认值)、NORTH、
NORTHEAST、EAST、SOUTHEAST、
WEST、NORTHWEST可供选择。
insets:设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,默认为
(0,0,0,0).
ipadx,ipady:设置组件内的间距,默认值为0。
我们以前提过,GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我
们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。
例子:
GridBagLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDemo{
public GridBagLayoutDemo(){
JButton b;
GridBagConstraints c;
int
gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double 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;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("second");
gridx=1;
gridy=0;
gridwidth=2;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=50;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("third");
gridx=0;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("fourth");
gridx=1;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("This is the last button");
gridx=2;
gridy=1;
gridwidth=1;
gridheight=2;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
f.setTitle("GridBagLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public static void main(String[] args){
new GridBagLayoutDemo();
}
}
GridBagLayout布局
总结了GridBagLayout的用法中的关键点如下:
1.要明确一点概念:每个 GridBagLayout对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为显示区域。
网格的总体方向取决于容器的 ComponentOrientation属性。对于水平的从左到右的方向,网格坐标(0,0)位于容器的左上角,其中 X向右递增,Y向下递增。
2.要使用GidBagLayout要先定义一个GridBagConstraints对象。
java API说明如下:“每个由 GridBagLayout管理的组件都与 GridBagConstraints的实例相关联。Constraints对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。”
例如,如下几行代码就可以添加其它组件:
GridBagLayout gridbag= new GridBagLayout();
GridBagConstraints c= new GridBagConstraints();
JFrame f=new JFrame();
f.setLayout(gridbag);
Button button= new Button(name);
gridbag.setConstraints(button, c);
f.add(jButton);
3.为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints对象。
即须设置GridBagConstraints对象的属性。我认为只要能掌握以下四种参数就能很好的使用GidBagLayout:
(1)GridBagConstraints.gridwidthGridBagConstraints.gridheight
指定组件的显示区域行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。如下向窗口中添加一个占两个单元格(两行一列)的按钮的例子:
JFrame f=new JFrame();
GridBagLayout gridbag= new GridBagLayout();
GridBagConstraints c= new GridBagConstraints();
f.setLayout(gridbag);
c.gridheight=2;
c.gridwidth=1;
JButton jButton= new JButton("按钮1");
gridbag.setConstraints(button, c);
f.add(jButton);
(2)GridBagConstraints.fill
当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。
可能的值为 GridBagConstraints.NONE(默认值)、
GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、
GridBagConstraints.VERTICAL(加高组件直到它足以在垂直方向上填满其显示区域,但不更改其宽度)和
GridBagConstraints.BOTH(使组件完全填满其显示区域)。
使用情景举例:在一个很大的窗口(如300*300)中添加一个按钮(原始大小40*30)。
(3)GridBagConstraints.anchor
当组件小于其显示区域时,用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的ComponentOrientation属性,而绝对值则不然。个人觉得只使用绝对值就可以。有效值有:
绝对值
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER(the default)
(4)GridBagConstraints.weightx、GridBagConstraints.weighty(************最重要的属性)
用于确定分布空间的方式,这对于指定调整行为至关重要。例如:在一个很大的窗口(如300*300)中添加两个按钮(也可以是面板)(原始大小40*30),默认的,你会发现两个按钮分别处于上下两个等大小的区域中,且只占用了一小部分,没有被按钮占用的区域就被称为额外区域。该额外区域会随着参数weightx、weighty而被分配。
完整的示例代码如下:
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Example{
public Example(){
}
public static void main(String args[]){
JFrame f= new JFrame("GridBag Layout Example");
GridBagLayout gridbag= new GridBagLayout();
GridBagConstraints c= new GridBagConstraints();
f.setLayout(gridbag);
//添加按钮1
c.fill= GridBagConstraints.BOTH;
c.gridheight=2;
c.gridwidth=1;
c.weightx=0.0;//默认值为0.0
c.weighty=0.0;//默认值为0.0
c.anchor=GridBagConstraints.SOUTHWEST;
JButton jButton1= new JButton("按钮1");
gridbag.setConstraints(jButton1, c);
f.add(jButton1);
//添加按钮2
c.fill= GridBagConstraints.NONE;
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=1;
c.weightx=1.0;//默认值为0.0
c.weighty=0.8;
JButton jButton2= new JButton("按钮2");
gridbag.setConstraints(jButton2, c);
f.add(jButton2);
//添加按钮3
c.fill= GridBagConstraints.BOTH;
c.gridwidth=1;
c.gridheight=1;
c.weighty=0.2;
JButton jButton3= new JButton("按钮3");
gridbag.setConstraints(jButton3, c);
f.add(jButton3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.setVisible(true);
}
}
在上述代码中添加按钮2时c.weighty=0.8,而在添加按钮3时c.weighty=0.2,这就会导致按钮2所占区域的高大约是按钮3所占区域的高的0.8/0.2=4倍。
很高兴为您回答!
求教:Java中GridBagLayout布局的使用
你这图哪用得到GridBagLayout,左边一个JPanel,用BoxLayout或Borderlayout就可以了,右边同理,话说右下角那个按钮哪有这样放的,这样已经覆盖那个文本域了。
给个GridBagLayout的代码你自己琢磨琢磨
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GridBagLayoutTest extends JFrame{
public GridBagLayoutTest(){
setLayout(new GridBagLayout());
GridBagConstraints constraints= new GridBagConstraints();
constraints.insets= new Insets(5,5,5,5);//top,left,bottom,right逆时针方向
JButton b= new JButton("One");
/*
*当窗体大小改变时,将额外的空间填充到网格单元中
* weightx为水平方向,weighty为垂直方向
*数值0.0~1.0,数值越大分配的空间越多
*/
constraints.weightx= 1.0;
constraints.weighty= 1.0;
constraints.gridx= 0;
constraints.gridy= 0;
constraints.gridwidth= 2;
add(b,constraints);
b= new JButton("Two");
constraints.gridy++;
add(b,constraints);
b= new JButton("Three");
constraints.anchor= GridBagConstraints.NORTH;//将组件放在某个方向上
constraints.gridx= 2;
constraints.gridy= 0;
constraints.gridwidth= 1;
constraints.gridheight= 2;
add(b,constraints);
b= new JButton("Four");
constraints.anchor= GridBagConstraints.CENTER;
constraints.gridx= 3;
constraints.gridheight= 1;
constraints.gridwidth= 2;
add(b,constraints);
b= new JButton("Five");
constraints.gridy= 1;
constraints.gridwidth= 2;
add(b,constraints);
JTextField text= new JTextField(40);
constraints.gridx= 0;
constraints.gridy= 2;
//使用REMAINDER可以使该组件无限延长到边缘
constraints.gridwidth= GridBagConstraints.REMAINDER;
constraints.gridheight= 1;
constraints.weighty= 0.0;
/*
*如果组件所在的单元格有剩余空间,则组件将自身填充满该单元格
*当weightx和weighty有分配值时anchor才有效
*可能的值有
* NONE默认
* VERTICAL
* HORIZONTAL
* BOTH
*/
constraints.fill= GridBagConstraints.HORIZONTAL;
add(text,constraints);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
//new GridBagLayoutTest();
new A();
}
}
/*
* anchor属性,将组件放在占单元格的某个固定方向上
*当fill在水平有效时,WEST和EAST对于anchor无效
*当fill在垂直有效时,NORTH和SOUTH对于anchor无效
*当weightx和weighty有分配值时anchor才有效
*/
@SuppressWarnings("serial")
class A extends JFrame{
public A(){
setLayout(new GridBagLayout());
GridBagConstraints c= new GridBagConstraints();
c.insets= new Insets(5,5,5,5);
c.gridx= 0;
c.gridy= 0;
c.gridwidth= 1;
c.gridheight= 1;
c.weightx= 1.0;
c.weighty= 1.0;
c.anchor= GridBagConstraints.NORTH;
JButton b= new JButton("N");
add(b,c);
b= new JButton("E");
c.gridx= 1;
c.gridy= 0;
c.anchor= GridBagConstraints.EAST;
add(b,c);
b= new JButton("S");
c.gridx= 1;
c.gridy= 1;
c.anchor= GridBagConstraints.SOUTH;
add(b,c);
b= new JButton("W");
c.fill= GridBagConstraints.VERTICAL;
c.gridx= 0;
c.gridy= 1;
c.anchor= GridBagConstraints.WEST;
add(b,c);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
如果你还想了解更多这方面的信息,记得收藏关注本站。