如上篇:Unmi 学习 Groovy 之 Groovy 和 Swing,Groovy 应用 Markup、命名参数、匿名构造和闭包很好的支持了 Swing 开发。然而 IBM 当初考虑到 Swing 的表现自行开发了一个界面框架,并已广受开发者青睐,也在 RCP 中得到了发展。Groovy 自然也看到了这一点,有了实际动作,见 http://groovy.codehaus.org/GroovySWT。它通过 SwtBuilder 和 JFaceBuilder 不令提供了对 Swt 的支持,还支持了 Swt 更上一层的 JFace。
要使用 GroovySWT,必须先下载到 groovy-swt-0.3.jar,或用 Eclipse 自带的 swt.jar,或从 Swt 官方网站(http://www.eclipse.org/swt/) 上下载最新的 swt.jar。或者直接下载打包的 groovy-swt-0.3-including-Eclipse-libs.ZIP,其中包含有 groovy-swt-0.3 和 swt/jface 等包。swt/jface 工程的 classpath 设置这里不多说,本文假定你对 Swt 有一定的了解,最好能理解 Swt 的 HelloWorld 程序。下面还是以例子进行说明吧。
Java-Swt 传统方式翻译为 groovy 的写法
1import org.eclipse.swt.SWT
2import org.eclipse.swt.widgets.*
3import org.eclipse.swt.layout.RowLayout as Layout
4
5def display = new Display()
6def shell = new Shell(display)
7
8shell.layout = new Layout(SWT.VERTICAL)
9
10shell.text = 'Groovy / SWT Test'
11
12def label = new Label(shell, SWT.NONE)
13label.text = 'Simple demo of Groovy and SWT'
14shell.defaultButton = new Button(shell, SWT.PUSH)
15shell.defaultButton.text = ' Push Me '
16
17shell.pack()
18shell.open()
19
20while (!shell.disposed) {
21 if (!shell.display.readAndDispatch()) shell.display.sleep()
22}
| 执行该程序的界面如下:
 |
使用 SwtBuilder 改写上面的程序,并加上了按钮事件处理的功能
1import org.eclipse.swt.SWT
2import org.eclipse.swt.widgets.*
3
4def swt = new groovy.swt.SwtBuilder()
5def shell = swt.shell(text:'Groovy / SWT Test') {
6 rowLayout()
7 label(style:"none", text:'Simple demo of Groovy and SWT')
8 button(style:"push", text:' Push Me ') {
9 onEvent(type:"Selection", closure:{ println "Hello" })
10 }
11}
12
13//对于下面这块代码并未能作出简化
14shell.pack()
15shell.open()
16
17while (!shell.disposed) {
18 if (!shell.display.readAndDispatch()) shell.display.sleep()
19}
| 执行该程序的界面如下:
在 button 的闭包中用 onEvent 指示了什么
类型的事件执行什么闭包。 |
从上面的程序对比上可看出使用了 SwtBuilder 只简化了界面构造的代码,并加上了处理按钮点击的闭包。闭包是作为 shell 的参数,在布局管理方面的代码写法不同于 SwingBuilder,如上篇的
tableLayout{
tr{
td(align:'center'){ label(text:'Username') }
td(align:'center'){ label(text:'Balance') }
}
}
而在 SwtBuilder 中 rowLayout() 和 Label() 是平级的,需要注意到这一点。
最后看一下 Groovy 结合 JFace 应用的例子,JFace 对于 Swt 就如 MFC 和 Windows SDK 的关系。其中用到了 JFaceBuilder。
1import groovy.jface.JFaceBuilder
2
3def jface = new JFaceBuilder()
4def mainapp
5//如果把 mainapp 定义和声明合在一起(def mainapp = jface.applicationWindows(),
6//则会在中间的 mainapp.setStatus('Hello ...') 引用 mainapp 时出错,
7//提示找不到当前类的 mainapp 属性
8
9//主应用程序窗口
10mainapp = jface.applicationWindow(title:"Groovy JFace Demo",
11 location:[400,300], size:[300, 200]) {
12
13 //主菜单
14 menuManager( text:"File" ) {//action: 就是一个菜单项,closure 为处理事件的闭包
15 action ( text:"Very Nice", closure:{ println "Very Nice !!!" } )
16 separator() //分隔线
17 action ( text:"Check me", checked:true,
18 closure:{ println "I've been checked" } )
19 }
20
21 menuManager( text:"Edit" ) {
22 action ( text:"Say Hi Statusbar",
23 closure:{ mainapp.setStatus('Hello ...') } )
24 action ( text:"Clear Statusbar",
25 closure:{ mainapp.setStatus('') } )
26 }
27
28 //工具栏
29 toolBarManager() {
30 action ( text:"&Open", closure:{ println "Open file pressed" } )
31 action ( text:"&C", closure:{ println "C pressed" } )
32 action ( text:"&R", closure:{ println "R pressed" } )
33 action ( text:"&A", closure:{ println "A pressed" } )
34 separator()
35 action ( text:"&Z", closure:{ println "Z pressed" } )
36 action ( text:"&Y", closure:{ println "Y pressed" } )
37 separator()
38 def act1 = action ( closure:{ println "last one" } )
39 }
40
41 fillLayout ( type:"vertical" )
42 label( text:"A big red label", background:[204, 0, 0] )
43 label( text:"I can barelly read this", foreground:[0,200,0] )
44 label( text:"It sure looks like the dutch flag",
45 foreground:[255,255,255], background:[0, 0, 153] )
46}
47
48mainapp.getMenuBarManager().updateAll(true)
49mainapp.getToolBarManager().update(true)
50mainapp.getShell().layout()
51mainapp.open()
| 执行该程序的界面如下:
一个基本的 JFace 应用框架,有主菜单栏,工具栏,主
显示区域和状态栏,可点击菜单看看效果,工具栏上的
按钮设置了快捷方式 |
更多的关天 GroovySwt 的例子请参见 http://svn.codehaus.org/groovy/trunk/groovy/modules/groovy-swt/src/examples/ 中的演示代码。上面的代码比较分散,为方便下载,本人特地整理打成一个包放在了这里:groovy-swt-examples.rar。
参考:http://groovy.codehaus.org/GroovySWT 及其中的例子。
本文链接 https://yanbin.blog/unmi-study-groovy-swt/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。