SWT中模拟AWT的BorderLayout
2008-01-05 08:25:48 来源:WEB开发网核心提示: BorderLayout 是 JFrame 的默认布局类,相信大家都用过,SWT中模拟AWT的BorderLayout,SWT没有提供这个java程序员非常熟悉的Layout类,我们怎们来自己定义一个呢?首先要稍微了解一下Layout的内部实现原理,这个属性通过 Control.setLayoutData() 方法
BorderLayout 是 JFrame 的默认布局类,相信大家都用过,SWT没有提供这个java程序员非常熟悉的Layout类。我们怎们来自己定义一个呢?首先要稍微了解一下Layout的内部实现原理。
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;
}
}
}
进入讨论组讨论。
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;
}
}
}
进入讨论组讨论。
[]
赞助商链接