网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  swt中模拟awt的borderlayout     
  文章作者:未知  文章来源:水木森林  
  查看:116次  录入:管理员--2007-11-17  
      borderlayout 是 jframe 的默认布局类,相信大家都用过,swt没有提供这个java程序员非常熟悉的layout类。我们怎们来自己定义一个呢?首先要稍微了解一下layout的内部实现原理。
 swt中模拟awt的borderlayout

   layouts 是一个容器用来对其子成员布局的一个算法,符合 strategy design pattern . 当 swt 打开一个 composite 时,会调用 composite 里的 layout.computesize() 计算 composite 的大小,然后再调 用 layout.layout() 设置子成员的位置和大小 . 如果需要, layout 会调用子成员的 getlayoutdata() 来获得单个子成员特别的属性。

computesize() 和 layout() 是抽象类 layout 的两个抽象方法。

   要定义一个新的 layout ,也就是要定义一个 layout 的子类, 实现 computesize 和 layout. 对 borderlayout 来说,我们需要区分子控件是在哪个位置的 , 是 west 的,还是 east 的,还是 center 的,这个属性通过 control.setlayoutdata() 方法保存的各个控件里。

废话少说了,先看源代码


code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->public   class  borderlayout  extends  layout {
     private  control north;
     private  control south;
     private  control east;
     private  control west;
     private  control center;

     protected   void  getcontrols(composite composite) {
        control[] children  =  composite.getchildren();
         for  ( int  i  =   0 , n  =  children.length; i  <  n; i ++ ) {
            control child  =  children[i];
            borderdata borderdata  =  (borderdata) child.getlayoutdata();
             if  (borderdata  ==  borderdata.north)
                north  =  child;
             else   if  (borderdata  ==  borderdata.south)
                south  =  child;
             else   if  (borderdata  ==  borderdata.east)
                east  =  child;
             else   if  (borderdata  ==  borderdata.west)
                west  =  child;
             else
                center  =  child;
        }
    }
}

control的layout data可以用control.setlayoutdata()方法设定, 所以getcontrol()方法找着各个控件的相应位置。



code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected  point computesize(composite composite,  int  whint,  int  hhint,
             boolean  flushcache) {
        getcontrols(composite);
         int  width  =   0 , height  =   0 ;

        width  +=  west  ==   null   ?   0  : getsize(west, flushcache).x;
        width  +=  east  ==   null   ?   0  : getsize(east, flushcache).x;
        width  +=  center  ==   null   ?   0  : getsize(center, flushcache).x;

         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            width  =  math.max(width, pt.x);
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            width  =  math.max(width, pt.x);
        }

        height  +=  north  ==   null   ?   0  : getsize(north, flushcache).y;
        height  +=  south  ==   null   ?   0  : getsize(south, flushcache).y;

         int  heightother  =  center  ==   null   ?   0  : getsize(center, flushcache).y;
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
        height  +=  heightother;

         return   new  point(math.max(width, whint), math.max(height, hhint));
    }

computesize计算composite所需的大小。


code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected   void  layout(composite composite,  boolean  flushcache) {
        getcontrols(composite);
        rectangle rect  =  composite.getclientarea();
         int  left  =  rect.x, right  =  rect.width, top  =  rect.y, bottom  =  rect.height;
         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            north.setbounds(left, top, rect.width, pt.y);
            top  +=  pt.y;
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            south.setbounds(left, rect.height  -  pt.y, rect.width, pt.y);
            bottom  -=  pt.y;
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            east.setbounds(rect.width  -  pt.x, top, pt.x, (bottom  -  top));
            right  -=  pt.x;
        }
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            west.setbounds(left, top, pt.x, (bottom  -  top));
            left  +=  pt.x;
        }
         if  (center  !=   null ) {
            center.setbounds(left, top, (right  -  left), (bottom  -  top));
        }
    }

而layout方法让控件们各归其位。整个布局调用是回归的。

完整的代码borderlayout.rar

control的layout data可以用control.setlayoutdata()方法设定, 所以getcontrol()方法找着各个控件的相应位置。



code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected  point computesize(composite composite,  int  whint,  int  hhint,
             boolean  flushcache) {
        getcontrols(composite);
         int  width  =   0 , height  =   0 ;

        width  +=  west  ==   null   ?   0  : getsize(west, flushcache).x;
        width  +=  east  ==   null   ?   0  : getsize(east, flushcache).x;
        width  +=  center  ==   null   ?   0  : getsize(center, flushcache).x;

         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            width  =  math.max(width, pt.x);
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            width  =  math.max(width, pt.x);
        }

        height  +=  north  ==   null   ?   0  : getsize(north, flushcache).y;
        height  +=  south  ==   null   ?   0  : getsize(south, flushcache).y;

         int  heightother  =  center  ==   null   ?   0  : getsize(center, flushcache).y;
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
        height  +=  heightother;

         return   new  point(math.max(width, whint), math.max(height, hhint));
    }

computesize计算composite所需的大小。


code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected   void  layout(composite composite,  boolean  flushcache) {
        getcontrols(composite);
        rectangle rect  =  composite.getclientarea();
         int  left  =  rect.x, right  =  rect.width, top  =  rect.y, bottom  =  rect.height;
         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            north.setbounds(left, top, rect.width, pt.y);
            top  +=  pt.y;
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            south.setbounds(left, rect.height  -  pt.y, rect.width, pt.y);
            bottom  -=  pt.y;
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            east.setbounds(rect.width  -  pt.x, top, pt.x, (bottom  -  top));
            right  -=  pt.x;
        }
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            west.setbounds(left, top, pt.x, (bottom  -  top));
            left  +=  pt.x;
        }
         if  (center  !=   null ) {
            center.setbounds(left, top, (right  -  left), (bottom  -  top));
        }
    }

而layout方法让控件们各归其位。整个布局调用是回归的。

完整的代码borderlayout.rar

control的layout data可以用control.setlayoutdata()方法设定, 所以getcontrol()方法找着各个控件的相应位置。



code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected  point computesize(composite composite,  int  whint,  int  hhint,
             boolean  flushcache) {
        getcontrols(composite);
         int  width  =   0 , height  =   0 ;

        width  +=  west  ==   null   ?   0  : getsize(west, flushcache).x;
        width  +=  east  ==   null   ?   0  : getsize(east, flushcache).x;
        width  +=  center  ==   null   ?   0  : getsize(center, flushcache).x;

         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            width  =  math.max(width, pt.x);
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            width  =  math.max(width, pt.x);
        }

        height  +=  north  ==   null   ?   0  : getsize(north, flushcache).y;
        height  +=  south  ==   null   ?   0  : getsize(south, flushcache).y;

         int  heightother  =  center  ==   null   ?   0  : getsize(center, flushcache).y;
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            heightother  =  math.max(heightother, pt.y);
        }
        height  +=  heightother;

         return   new  point(math.max(width, whint), math.max(height, hhint));
    }

computesize计算composite所需的大小。


code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

-->     protected   void  layout(composite composite,  boolean  flushcache) {
        getcontrols(composite);
        rectangle rect  =  composite.getclientarea();
         int  left  =  rect.x, right  =  rect.width, top  =  rect.y, bottom  =  rect.height;
         if  (north  !=   null ) {
            point pt  =  getsize(north, flushcache);
            north.setbounds(left, top, rect.width, pt.y);
            top  +=  pt.y;
        }
         if  (south  !=   null ) {
            point pt  =  getsize(south, flushcache);
            south.setbounds(left, rect.height  -  pt.y, rect.width, pt.y);
            bottom  -=  pt.y;
        }
         if  (east  !=   null ) {
            point pt  =  getsize(east, flushcache);
            east.setbounds(rect.width  -  pt.x, top, pt.x, (bottom  -  top));
            right  -=  pt.x;
        }
         if  (west  !=   null ) {
            point pt  =  getsize(west, flushcache);
            west.setbounds(left, top, pt.x, (bottom  -  top));
            left  +=  pt.x;
        }
         if  (center  !=   null ) {
            center.setbounds(left, top, (right  -  left), (bottom  -  top));
        }
    }

而layout方法让控件们各归其位。整个布局调用是回归的。

完整的代码borderlayout.rar

 
 
上一篇: cglib实现变化字段探测的供能    下一篇: 用jni实现一个高精度的java计时器
  相关文档
createfolder 方法 11-16
课程介绍(1):sl-110 初级java语言编程 11-17
sql命令:导入导出 11-17
java入门 java中的日期处理 11-17
详细介绍在tomcat中配置数据源原理 11-17
java规则引擎工作原理及应用 11-17
深入探讨sql server 2000对xml的支持(四) 11-17
static和final修饰类属性变量及初始化 12-12
jcreator pro 2.5与wtk2.2配置(上) 11-17
在j2me程序中实现字符串分割 11-16
一个简单的timer service 11-16
全面介绍xen虚拟机 深入学习xen新起点 11-17
struts入门经验(二) 11-17
java中的string、stringbuffer和math类 11-17
关于如何用java动态代理实现aop的技术说明 04-14
java手机软件图形界面api之低级gui组件 11-16
java认证考试的前期准备 11-17
数学能力高低对编程工作者有很大影响 11-16
使用ant进行web开发, 第一部分 11-17
java学习日记(线程) 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息