First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-179  评论-1027  文章-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 山风小子 阅读(1953) 评论(3)  编辑  收藏 所属分类: Groovy & Grails

评论:
# re: Groovy高效编程——@Bindable的使用 2008-05-05 10:15 | flyisland
这个写法跟Flex很相似啊  回复  更多评论
  
# re: Groovy高效编程——@Bindable的使用 2008-05-05 21:30 | 山风小子
@flyisland
没用过Flex :(  回复  更多评论
  
# re: Groovy高效编程——@Bindable的使用 2009-03-13 11:07 | aloxc
此群已满,新的groovy群28816999  回复  更多评论
  


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交