| |
borderlayout 是 jframe 的默认布局类,相信大家都用过,swt没有提供这个java程序员非常熟悉的layout类。我们怎们来自己定义一个呢?首先要稍微了解一下layout的内部实现原理。
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
|
|