从 Groovy SwingBuilder 中的 Swing 组件获取 ID

Get id from Swing component in Groovy SwingBuilder

假设我使用 SwingBuilder 声明一个对象如下:

swing.button(id:'something')

我知道我可以通过调用从 SwingBuilder 取回 Component

swing."something"

但是我如何从 Component 返回 的实例作为字符串返回其 id

Component c = getMyButton()
String whatIWant = c.id    //Property doesn't exist
String attempt2  = c['id'] //Property doesn't exist

我相信你要找的是 name,所以 Component#getName() 应该做你想做的。

例如:

Component c = getMyButton()
String whatIWant = c.getName() // or c.name

不幸的是,这没有记录,但是 it appears to be what the Groovy SwingBuilder source does, though perhaps only when there is no explicit .id property(在 Component..? 上不存在)