站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse插件开发系列5.SWT中的布局管理器(4)

Eclipse插件开发系列5.SWT中的布局管理器(4)

摘要: Eclipse插件开发系列5.SWT中的布局管理器(4)

前言:

  这个系列的文章又停了两周,因为最近玩心甚重,双休去北京周边去玩,花去不少时间。昨日MSN上有朋友说:“他们的系统用SWING来开发太丑了,决定改用SWT来做,可是这方面的资料还是太少了。”于是我再次提笔续写下去。Eclipse不仅仅是象Jbuilder一样的开发工具,它本身也是一个非常不错的Java Application开发平台,现在所流行构件式开发的概念在Eclipse得到了最好的体现。当你深入到Eclipse插件的开发中你也会更深入的体会到什么叫做面向对象。在这里感谢大家对这一系列文章的支持,希望尽量转帖,以传播Eclipse插件的开发方式,但请转帖时,保持文章的完整性及作者声明,尊重作者的劳动。

*************************************************************************************
作者:陈刚,桂林人,97年毕业于广西师范大学数学系,专注于java平台。现(2004.2-?)暂在IBM中国研究中心进行Eclipse插件的开发。
Email:
glchengang@163.com
blog: glchengang.yeah.net
*************************************************************************************

  在前面几节我们介绍了RowLayout、FillLayout、GridLayout、StackLayout,这些管理器足够我们用的了。这是布局管理器的最后最一节,我们来做一个复杂一点的界面,这个界面取自我正在开发中的一个项目---PPP的报表模块的主界面。在这一节将展示如何利用SWT Designer插件软件(此插件第前面几章有过使用介绍)来进行界面编程,希望对大家有所帮助。

报表模块的主界面如下图:

1、初步构架。做复杂界面,首先要将界面进行板块的分割,要“分而治之”,不要把所有控件全放在一个面板中,这样会很混乱,今后维护会非常困难。在这里我们将此界面划分成如下图的红色框的四个大块。

这四个板块由三个Group和一个Compsite组成,我们先写出大的构架出来,效果如下图:

其代码如下:

/*
* 陈刚 ,创建日期 2004-6-21
*
* Email: glchengang@yeah.net
* Blog : glchengang.yeah.net
*/
package net.yeah.glchengang.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;


public class LastApp {

public static void main(String[] args) {
LastApp window = new LastApp();
window.open();
}
public void open() {
final Display display = new Display();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setText("SWT Application");
{
final Composite composite = new Composite(shell, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
{
final Group group = new Group(composite, SWT.NONE);
group.setText("aaa");
group.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
group.setLayout(new GridLayout());
}
{
final Group group = new Group(composite, SWT.NONE);
group.setText("bbb");
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setLayout(new GridLayout());
}
{
final Group group = new Group(composite, SWT.NONE);
group.setText("ccc");
final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
group.setLayoutData(gridData);
group.setLayout(new GridLayout());
}
{
final Button button = new Button(composite, SWT.CHECK);
button.setText("check button");
}
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}

2、逐步细化。接下来就是在第一步的基础上将更多的控件加入进去。效果如下图。

代码如下:

/*
* 陈刚 ,创建日期 2004-6-21
*
* Email: glchengang@yeah.net
* Blog : glchengang.yeah.net
*/
package net.yeah.glchengang.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;


public class LastApp {

public static void main(String[] args) {
LastApp window = new LastApp();
window.open();
}
public void open() {
final Display display = new Display();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setText("SWT Application");
{
final Composite composite = new Composite(shell, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
{
final Group group = new Group(composite, SWT.NONE);
group.setText("aaa");
final GridData gridData_1 = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData_1.widthHint = 160;
gridData_1.heightHint = 150;
group.setLayoutData(gridData_1);
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 2;
group.setLayout(gridLayout_1);
{
final Tree tree = new Tree(group, SWT.BORDER);
final GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
tree.setLayoutData(gridData);
}
{
final Button button = new Button(group, SWT.NONE);
button.setText("button");
}
{
final Button button = new Button(group, SWT.NONE);
button.setText("button");
}
}
{
final Group group = new Group(composite, SWT.NONE);
group.setText("bbb");
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
group.setLayout(new GridLayout());
{
final Label label = new Label(group, SWT.NONE);
label.setText("label");
}
{
new Combo(group, SWT.NONE);
}
{
final Label label = new Label(group, SWT.NONE);
label.setText("label");
}
{
new Combo(group, SWT.NONE);
}
}
{
final Group group = new Group(composite, SWT.NONE);
group.setText("ccc");
final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
group.setLayoutData(gridData);
group.setLayout(new GridLayout());
{
final Composite composite_1 = new Composite(group, SWT.NONE);
final GridData gridData_1 = new GridData(GridData.FILL_HORIZONTAL);
composite_1.setLayoutData(gridData_1);
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 3;
composite_1.setLayout(gridLayout_1);
{
final Label label = new Label(composite_1, SWT.NONE);
label.setText("label");
}
{
final Button button = new Button(composite_1, SWT.RADIO);
button.setText("radio button");
}
{
final Button button = new Button(composite_1, SWT.RADIO);
button.setText("radio button");
}
}
{
final Composite composite_1 = new Composite(group, SWT.NONE);
composite_1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 2;
composite_1.setLayout(gridLayout_1);
{
final Label label = new Label(composite_1, SWT.NONE);
label.setText("label");
}
{
final Text text = new Text(composite_1, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
}
{
final Composite composite_1 = new Composite(group, SWT.NONE);
composite_1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 3;
composite_1.setLayout(gridLayout_1);
{
final Label label = new Label(composite_1, SWT.NONE);
label.setText("label");
}
{
final Text text = new Text(composite_1, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
{
final Button button = new Button(composite_1, SWT.NONE);
button.setText("button");
}
}
}
{
final Button button = new Button(composite, SWT.CHECK);
button.setText("check button");
}
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}

3、优化代码。以上两步的代码都是用SWT Desiger插件自动生成的,代码经过优化编辑后如下,这样就简洁多了:

/*
* 陈刚 ,创建日期 2004-6-21
*
* Email: glchengang@yeah.net
* Blog : glchengang.yeah.net
*/
package net.yeah.glchengang.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;

public class LastApp {

public static void main(String[] args) {
LastApp window = new LastApp();
window.open();
}
public void open() {
Display display = new Display();
Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setText("SWT Application");
{

Composite c = new Composite(shell, SWT.NONE);
c.setLayout(new GridLayout(2, false)); //注意这种生成GridLayout的方式,为我们节省了二行代码
{
Group group = new Group(c, SWT.NONE);
group.setText("aaa");
GridData gd2 = new GridData();
gd2.widthHint = 160;
gd2.heightHint = 150;
group.setLayoutData(gd2);
group.setLayout(new GridLayout(2, false));
{
Tree tree = new Tree(group, SWT.BORDER);
GridData gd3 = new GridData(GridData.FILL_BOTH);
gd3.horizontalSpan = 2; //设置tree横占2格
tree.setLayoutData(gd3);
/*
* 这是一种快捷写法,节省了一行代码,但一般用于label型这种无须对其对象再引用的情况
* 一般来说都是要引用Button对象的,这种写法只是暂时的。
*/
new Button(group, SWT.NONE).setText("button");
new Button(group, SWT.NONE).setText("button");
}
}
{
Group group = new Group(c, SWT.NONE);
group.setText("bbb");
//横向抢占式、纵向对齐式
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
group.setLayout(new GridLayout());
{
new Label(group, SWT.NONE).setText("label");
new Combo(group, SWT.NONE);
new Label(group, SWT.NONE).setText("label");
new Combo(group, SWT.NONE);
}
}
{
Group group = new Group(c, SWT.NONE);
group.setText("ccc");
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
group.setLayoutData(gridData);
group.setLayout(new GridLayout());
{
Composite c2 = new Composite(group, SWT.NONE);
c2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
c2.setLayout(new GridLayout(3, false));
{
new Label(c2, SWT.NONE).setText("label");
new Button(c2, SWT.RADIO).setText("radio button");
new Button(c2, SWT.RADIO).setText("radio button");
}
}
{
Composite c2 = new Composite(group, SWT.NONE);
c2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
c2.setLayout(new GridLayout(2, false));
{
new Label(c2, SWT.NONE).setText("label");

Text text = new Text(c2, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
}
{
Composite c2 = new Composite(group, SWT.NONE);
c2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
c2.setLayout(new GridLayout(3, false));
{
new Label(c2, SWT.NONE).setText("label");

Text text = new Text(c2, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

new Button(c2, SWT.NONE).setText("button");
}
}
}

new Button(c, SWT.CHECK).setText("check button");

}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}

4、最后,你就需要加入事件控制和你的系统商业逻辑了,这和界面的布局没有什么关系,就此略过。

  



↑返回目录
前一篇: 今天开始学Eclipse/SWT(1)
后一篇: Eclipse插件开发系列5.SWT中的布局管理器(3)