用WindowBuilder编写SWT 桌面应用程序
WindowBuilder是一款基于Eclipse平台的双向Java的GUI设计插件式的软件。它具备SWT/JFACE开发、Swing开发及GWT 开发三大功能,是一款不可多得的Java体系中的WYSIWYG工具。
1 安装WindowBuilder
可以从http://download.csdn.net/detail/sahusoft/9571016 ,下载安装包。然后就是按照eclipse安装插件的形式安装即可。即: Help/ Install New Software...
2 创建项目
File/New/Other..



在Design窗口中,你可以使用拖拽的方式很容易的使用各种部件。

3 设置布局
SWT库中几个典型的布局有”FillLayout, RowLayout, GridLayout
3.1 FillLayout
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
shell.setLayout(fillLayout);

用WindowBuilder的话,鼠标点击在你要设置的组件的上,然后右键有个setLayout的选项,然后选择你要设置的类型即可。
Source下的源码如下:
FillLayoutExample.java
package org.o7planning.tutorial.swt.layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FillLayoutExample {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
//
Composite parent = new Composite(shell, SWT.NONE);
FillLayout fillLayout= new FillLayout();
fillLayout.type= SWT.VERTICAL;
parent.setLayout(fillLayout);
Button b1 = new Button(parent, SWT.NONE);
b1.setText("B1");
Button b2 = new Button(parent, SWT.NONE);
b2.setText("B2");
Button button3 = new Button(parent, SWT.NONE);
button3.setText("Button 3");
// Windows back to natural size.
shell.pack();
//
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
// tear down the SWT window
display.dispose();
}
}
运行结果:

3.2 RowLayout
属性的配置如下:


3.3 GridLayout
GridLayout使用频率最大。配置参数

如果需要最大化自适应啥的,课通过设置LayoutData实现。