java程序设计表单,Eclipse Form程序设计指南(1)-JSP教程,Java技巧及代码

1、介绍

l         eclipse form是eclipse 3.0的新特性

l         eclipse form是一组定制小部件和支持类组成的插件,以前由pde和update组件内部使用,在eclipse 3.0中已经成为公用api

l         eclipse form提供:

n         适合包含到内容区域(编辑器或视图)中的“form”概念

n         用来管理颜色、超链接组和其它象swt控件一样的form外观的工具包

n         象html表格一样布局的新布局管理器

n         为form设计的定制控件(超链接、图像链接、可滚动的composite等)

n         每页是一个form的多页编辑器(就象pde)

2、快速入门

(1)helloworld例子

l         下面的例子在视图中创建一个空的form

public class formview extends viewpart {

private formtoolkit toolkit;

private scrolledform form;

public void createpartcontrol(composite parent) {

toolkit = new formtoolkit(parent.getdisplay());

form = toolkit.createscrolledform(parent);

form.settext("hello, eclipse forms");

}

public void setfocus() {

form.setfocus();

}

public void dispose() {

toolkit.dispose();

super.dispose();

}

}

l         首先要创建formtoolkit对象实例

l         由formtoolkit创建form对象(这里是scrolledform)

l         调用scrolledform的settext()方法,在form的顶部设置标题内容

l         注意:最后要dispose管理资源的formtoolkit对象

l         要在workbench中运行,需要plugin.xml中,将org.eclipse.ui.forms添加到必需插件列表,并注册视图

id="formsamples"

name="formsamples plug-in"

version="1.0.0"

provider-name="nelson_tu"

class="org.xqtu.samples.formsamplesplugin">

point="org.eclipse.ui.views">

class="org.xqtu.samples.views.formview"

name="form sample"

id="formview"/>

(2)添加内容

public void createpartcontrol(composite parent) {

toolkit = new formtoolkit(parent.getdisplay());

form = toolkit.createscrolledform(parent);

form.settext("hello, eclipse forms");

composite body = form.getbody();

gridlayout layout = new gridlayout();

body.setlayout(layout);

hyperlink link = toolkit.createhyperlink(body, "click here.",

swt.wrap);

link.addhyperlinklistener(new hyperlinkadapter() {

public void linkactivated(hyperlinkevent e) {

system.out.println("link activated!");

}

});

}

l         首先获得form的体内容,它是一个composite对象

l         设置其布局为gridlayout

l         通过formtoolkit创建一个超链接(hyperlink)控件

l         添加超链接事件侦听器,响应超链接的点击

(3)添加通用控件

l         由于form的体内容是一个composite对象,所以允许在其中创建swt控件

l         但是swt控件是被设计为适合窗口、对话框的,所以在form中使用是有问题的

l         在form中,使用formtoolkit创建对应的通用控件

public void createpartcontrol(composite parent) {

toolkit = new formtoolkit(parent.getdisplay());

form = toolkit.createscrolledform(parent);

form.settext("hello, eclipse forms");

composite body = form.getbody();

gridlayout layout = new gridlayout();

body.setlayout(layout);

hyperlink link = toolkit.createhyperlink(body, "click here.",

swt.wrap);

link.addhyperlinklistener(new hyperlinkadapter() {

public void linkactivated(hyperlinkevent e) {

system.out.println("link activated!");

}

});

layout.numcolumns = 2;

griddata gd = new griddata();

gd.horizontalspan = 2;

link.setlayoutdata(gd);

label label = toolkit.createlabel(body, "text field label:");

text text = toolkit.createtext(body, "");

text.setlayoutdata(new griddata(griddata.fill_horizontal));

text.setdata(formtoolkit.key_draw_border, formtoolkit.text_border);

button button = toolkit.createbutton(body,

"an example of a checkbox in a form", swt.check);

gd = new griddata();

gd.horizontalspan = 2;

button.setlayoutdata(gd);

toolkit.paintbordersfor(body);

}

l         上面的例子添加了三个通用控件:label、text和checkbox

l         由于缺省创建的text控件的外观是3d的,而要达到象pde一样的flat外观,需要做些额外工作:

n         调用setdata()方法,添加重画边框的附加信息

n         调用formtoolkit的paintbordersfor()方法重画flat外观的边框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值