作者:山风小子 来源:BlogJava   酷勤网收集 2008-05-03

摘要
  在Groovy 1.6.0 BETA 1中引入了@Bindable这一新的Annotation,该Annotation在Swing编程中使用起来尤为方便,下面给出一个实例供大家参考。其内部的实现原理应该是基于Observer模式的。点击‘update’按钮,随机更新label的值。

在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高效编程——@Bindable的使用

分类: 编程语言 程序人生 修炼之道

上一篇:C#与Java之比较   下一篇:Groovy 1.6.0 BETA 1 发布了!性能有显著提升!