网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 新手入门 > 开发工具 > Eclipse开发经典教程:SWT布局  

Eclipse开发经典教程:SWT布局

2008-02-27  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

自定义布局

在SWT中,用户可以通过setLayout设置组件的布局信息。布局对象会根据父组件的大小和子组件的布局信息计算出每个子组件的位置和大小,使整个布局空间符合用户的需求。下面将介绍如何创建自己的布局类,实现用户自定义的布局。

中国网管论坛bbs.bitsCN.com

Layout类

网管bitscn_com

在SWT中,所有的布局类都继承于Layout抽象类。Layout有两个抽象方法。

网管网www_bitscn_com

 1. computeSize (Composite composite, int wHint, int hHint, boolean flushCache)
2. layout (Composite composite, boolean flushCache)
网管u家u.bitscn@com

computeSize方法负责计算组件所有子组件所占的高度和宽度,并返回一个Point类型的变量(width,height)。layout方法负责计算子组件的大小和位置,并按计算出来的位置排列子组件。

中国网管联盟bitsCN.com

创建自己的布局类 网管联盟bitsCN_com

如果用户希望组件按自己的方式进行布局,可以创建自己的布局类,实现自己的布局。要实现自己的布局,用户要继承Layout类,并实现layout方法和computeSize方法。下面将实现一个简单的按列进行布局的布局类,在此布局中,所有的子组件将按一列显示,并且子组件的宽度相等,代码如例程4所示。

网管网www.bitscn.com

例程4 ColumnLayout.java 网管网www_bitscn_com

 public class ColumnLayout extends Layout {
public static final int MARGIN = 4;
public static final int SPACING = 2;
Point [] sizes;
int maxWidth, totalHeight;
protected Point computeSize(Composite composite, int wHint, int hHint,
boolean flushCache) {
Control children[] = composite.getChildren();
if (flushCache || sizes == null || sizes.length != children.length) {
initialize(children);
}
int width = wHint, height = hHint;
if (wHint == SWT.DEFAULT) width = maxWidth;
if (hHint == SWT.DEFAULT) height = totalHeight;
return new Point(width + 2 * MARGIN, height + 2 * MARGIN);
}
protected void layout(Composite composite, boolean flushCache) {
Control children[] = composite.getChildren();
if (flushCache || sizes == null || sizes.length != children.length) {
initialize(children);
}
Rectangle rect = composite.getClientArea();
网管u家u.bitscn@com

int x = MARGIN, y = MARGIN;
//计算最大宽度
int width = Math.max(rect.width - 2 * MARGIN, maxWidth);
for (int i = 0; i < children.length; i++) {
int height = sizes[i].y;
//设置子组件的位置
children[i].setBounds(x, y, width, height);
//计算当前组件的y轴的坐标
y += height + SPACING;
}
}
void initialize(Control children[]) {
maxWidth = 0;
totalHeight = 0;
sizes = new Point [children.length];
for (int i = 0; i < children.length; i++) {
//计算子组件的大小
sizes[i] = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
maxWidth = Math.max(maxWidth, sizes[i].x);
totalHeight += sizes[i].y;
}
totalHeight += (children.length - 1) * SPACING;
}
}
网管bitscn_com

在ColumnLayout类中,通过layout方法对子组件重新计算位置,并设置子组件的位置。为了验证ColumnLayout类,下面通过ColumnLayoutTest类测试ColumnLayout类的效果,代码如例程5所示。

网管下载dl.bitscn.com

例程5 ColumnLayoutTest.java

中国网管论坛bbs.bitsCN.com

 public class ColumnLayoutTest {
static Shell shell;
static Button button3;
public static void main(String[] args) {
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new ColumnLayout());
new Button(shell, SWT.PUSH).setText("B1");
new Button(shell, SWT.PUSH).setText("Very Wide Button 2");
(button3 = new Button(shell, SWT.PUSH)).setText("Button 3");
new Text(shell, SWT.NONE).setText("text");
Button grow = new Button(shell, SWT.PUSH);
grow.setText("Grow Button 3");
// 添加选择组件监听器
grow.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
button3.setText("Extreemely Wide Button 3");
//组件大小改变后通知父组件进行重新布局
shell.layout();
shell.pack();
}
});
Button shrink = new Button(shell, SWT.PUSH);
shrink.setText("Shrink Button 3");

网管联盟bitsCN@com


shrink.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
button3.setText("Button 3");
//组件大小改变后通知父组件进行重新布局
shell.layout();
shell.pack();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}

当选择“Grow Button 3”组件后,layout方法会根据子组件的最大宽度调整所有子组件的宽度,程序运行效果如图6所示。 网管联盟bitsCN_com

             
原始大小                                           宽度改变后
图6 自己定义布局

网管网www.bitscn.com

本节通过实例介绍了几种常用的布局方式,读者可以通过这几种布局方式实现SWT中大多数的布局需求。另外还有一种常用的布局FormLayout,有兴趣读者可以自行研究,在这里不一一介绍。

中国网管联盟bitsCN.com

如果有某些比较特殊的要求,读者可以尝试修改布局类,以适应相关的布局。读者应该掌握如何设置组件相应的布局信息,掌握如何使用几种方式进行布局,特别是GridLayout布局方式。在有特殊需要的时候要能够修改布局类以适应自己的要求。

网管u家u.bitsCN.com

网管论坛bbs_bitsCN_com


TAGs
 上一篇:只支持单表映射持久化框架──EasyDBO   下一篇:没有了
Eclipse开发经典教程:SWT布局 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: