使用groovy和SwingBuilder更改文本字段中的值 [英] Changing value in textfield with groovy and SwingBuilder

查看:136
本文介绍了使用groovy和SwingBuilder更改文本字段中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将变量(== i)的值放在文本字段中,以便将其值显示在文本字段中,即从1更改为10.

I want to put the value of a variable (==i) in a textfield, so that its value is shown in the textfield, i.e., changing from 1 to 10.

def sb = new SwingBuilder()
def th = Thread.start {
    for(i in 1..10) {
        sleep 2000
    }
}
def Pan = sb.panel(layout: new BorderLayout()) {
    sb.panel(constraints: BorderLayout.NORTH){
        gridLayout(cols: 2, rows: 3)
        textField id:'tf', text: ?
    }
}

推荐答案

您可以使用

You can do that with the doOutside method of SwingBuilder, which allows to run a closure outside the EDT. The code below does what you are trying to do (with a table layout instead of a grid layout).

import groovy.swing.SwingBuilder  
import static javax.swing.JFrame.EXIT_ON_CLOSE  
import java.awt.*

def swingBuilder = new SwingBuilder()

swingBuilder.edt {   
    def message

    def setMessage = { String s -> message.setText(s) }

    frame(title: 'Example', size: [200, 150], show: true, locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE) { 
        borderLayout(vgap: 5)

        panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
            tableLayout(cellpadding: 5) {
                tr {
                    td {
                        label 'Value'  // text property is default, so it is implicit.
                    }
                    td {
                        message = textField(id: 'tf', columns: 5, text: '0')
                    }
                }
            }           
        }
    }

    doOutside {
        for (i in 1..10) {
            sleep 1000
            edt { setMessage(String.valueOf(i)) }
        }
    }
}

这篇关于使用groovy和SwingBuilder更改文本字段中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆