【问题标题】:Groovy: How to access objects with Id tag?Groovy:如何使用 Id 标签访问对象?
【发布时间】:2009-04-22 15:02:47
【问题描述】:

我有以下 Groovy+SwingBuilder 代码。

在一个面板中我生成复选框,在另一个面板中我想访问复选框的值。代码看起来基本上是这样的:

def optionsmap = [ foo : "value_foo", bar : "value_bar"]

SwingBuilder.build()     
{
   frame(title:'demo1', size:[400,700], visible:true, defaultCloseOperation:WC.EXIT_ON_CLOSE) {                
      gridLayout(rows: 1, cols: 2)

      panel(id:'optionPanel', constraints:java.awt.BorderLayout.CENTER) {                        
         gridLayout(rows: 5, cols: 1)                 
         myGroup = buttonGroup();    
         for (entry in optionsmap)
         {         
           checkBox(id: entry.key,   text: entry.value        )                  
         }            
      }

      panel(constraints:java.awt.BorderLayout.WEST) {   

         button ('Show values', actionPerformed: {           
         for (entry in optionsmap)
         {
            println (entry.key as Class).text            
         }           
       })                
    }
  }  
}

optionsmap 是一个可以扩展的 (id, text) 对的映射。

当我按下 显示值 时,我收到一条错误消息:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: 不能 将具有类“java.lang.String”的对象“foo”转换为类 'java.lang.Class'

如何使用optionsmap 中的复选框 ID 在第二个面板中访问我的操作的复选框?

【问题讨论】:

  • “不起作用”到底是什么意思?它会崩溃吗?它可能只输出被检查的那些吗?都是没有被选中的吗?您如何绑定上一个面板表单中的数据?请更完整。

标签: model-view-controller groovy


【解决方案1】:

从map中访问变量的解决方案是这样的:


                 for (entry in optionsmap)
                 {
                      if (variables[entry.key].selected)
                          println variables[entry.key].text
         }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2022-11-14
    • 2015-09-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多