随笔 - 303  文章 - 883  trackbacks - 0
<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

欢迎光临! 
闲聊 QQ:1074961813

随笔分类(357)

我管理的群

公共blog

  • n维空间
  • Email : java3d@126.com 群 : 12999758

参与管理的论坛

好友的blog

我的其他blog

朋友的网站

搜索

  •  

最新评论

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使用。

代码:

  1import java.awt.*;
  2import java.awt.event.*;
  3import javax.swing.*;
  4public class GridBagLayoutDemo{
  5   public GridBagLayoutDemo(){
  6     JButton b;
  7     GridBagConstraints c;
  8     int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
  9     double weightx,weighty;
 10     Insets inset;
 11     
 12     JFrame f=new JFrame();
 13     
 14     GridBagLayout gridbag=new GridBagLayout();
 15       Container contentPane=f.getContentPane();
 16       contentPane.setLayout(gridbag);
 17       
 18       b=new JButton("first");
 19       gridx=0;
 20       gridy=0;
 21       gridwidth=1;
 22       gridheight=1;
 23       weightx=10;
 24       weighty=1;
 25       anchor=GridBagConstraints.CENTER;
 26       fill=GridBagConstraints.HORIZONTAL;
 27       inset=new Insets(0,0,0,0);
 28       ipadx=0;
 29       ipady=0;
 30       c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
 31          fill,inset,ipadx,ipady);
 32       gridbag.setConstraints(b,c);
 33       contentPane.add(b);
 34       
 35       
 36       b=new JButton("second");
 37       gridx=1;
 38       gridy=0;
 39       gridwidth=2;
 40       gridheight=1;
 41       weightx=1;
 42       weighty=1;
 43       anchor=GridBagConstraints.CENTER;
 44       fill=GridBagConstraints.HORIZONTAL;
 45       inset=new Insets(0,0,0,0);
 46       ipadx=50;
 47       ipady=0;
 48       c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
 49          fill,inset,ipadx,ipady);
 50       gridbag.setConstraints(b,c);
 51       contentPane.add(b);
 52
 53       b=new JButton("third");
 54       gridx=0;
 55       gridy=1;
 56       gridwidth=1;
 57       gridheight=1;
 58       weightx=1;
 59       weighty=1;
 60       anchor=GridBagConstraints.CENTER;
 61       fill=GridBagConstraints.HORIZONTAL;
 62       inset=new Insets(0,0,0,0);
 63       ipadx=0;
 64       ipady=50;
 65       c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
 66          fill,inset,ipadx,ipady);
 67       gridbag.setConstraints(b,c);
 68       contentPane.add(b);
 69       
 70       b=new JButton("fourth");
 71       gridx=1;
 72       gridy=1;
 73       gridwidth=1;
 74       gridheight=1;
 75       weightx=1;
 76       weighty=1;
 77       anchor=GridBagConstraints.CENTER;
 78       fill=GridBagConstraints.HORIZONTAL;
 79       inset=new Insets(0,0,0,0);
 80       ipadx=0;
 81       ipady=0;
 82       c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
 83          fill,inset,ipadx,ipady);
 84       gridbag.setConstraints(b,c);
 85       contentPane.add(b);
 86
 87       b=new JButton("This is the last button");
 88       gridx=2;
 89       gridy=1;
 90       gridwidth=1;
 91       gridheight=2;
 92       weightx=1;
 93       weighty=1;
 94       anchor=GridBagConstraints.CENTER;
 95       fill=GridBagConstraints.HORIZONTAL;
 96       inset=new Insets(0,0,0,0);
 97       ipadx=0;
 98       ipady=50;
 99       c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
100          fill,inset,ipadx,ipady);
101       gridbag.setConstraints(b,c);
102       contentPane.add(b);
103
104       f.setTitle("GridBagLayout");
105       f.pack();
106       f.setVisible(true);
107      f.addWindowListener(
108           new WindowAdapter(){
109               public void windowClosing(WindowEvent e){
110                  System.exit(0); 
111               }
 
112           }
 
113      );      
114   }

115   public static void main(String[] args){
116     new GridBagLayoutDemo();
117   }

118}

119



地震让大伙知道:居安思危,才是生存之道。
posted on 2007-03-03 11:22 小寻 阅读(328) 评论(0)  编辑  收藏 所属分类: j2se/j2ee/j2me

只有注册用户登录后才能发表评论。


网站导航: