分享
 
 
 

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

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

前言:

这个系列的文章又停了两周,因为最近玩心甚重,双休去北京周边去玩,花去不少时间。昨日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、最后,你就需要加入事件控制和你的系统商业逻辑了,这和界面的布局没有什么关系,就此略过。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有