First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-221  评论-1047  文章-0  trackbacks-0
在Groovy 1.6.0 BETA 1中引入了@Bindable这一新的Annotation,该Annotation在Swing编程中使用起来尤为方便,下面给出一个实例供大家参考。其内部的实现原理应该是基于Observer模式的。

点击‘update’按钮,随机更新label的值。请注意,我们仅仅设置了myBean的属性prop的值,未对label进行任何的显式操作(比如setText)

import groovy.beans.Bindable
import groovy.swing.*
import javax.swing.*
import java.awt.*

class MyBean {
    @Bindable 
    String prop
}

def rand 
= new Random()
def greetings 
= ['hello, world''hello, Groovy''hello, 山风小子']
def myBean 
= new MyBean(prop:greetings[2])
def swing 
= new SwingBuilder()
def frame 
= swing.frame(title:'Bindable Demo', location: [100100], size:[300100]) {
    panel(layout: 
new GridLayout(12)) {
        label(text:bind(source:myBean, sourceProperty:
'prop'))
        button(action(name:
'update', closure: {myBean.prop = greetings[rand.nextInt(3)]}))
    }
}

frame.setVisible(
true)


附:朝花夕拾——Groovy & Grails
posted on 2008-05-03 16:35 山风小子 阅读(2725) 评论(3)  编辑  收藏 所属分类: Groovy & Grails