Groovy SwingBuilder:更改面板颜色的按钮

发布于 2024-11-15 22:01:51 字数 1213 浏览 4 评论 0原文

为什么这段代码会失败? 我想更改动态构建的一系列多个面板中的一个面板的颜色(面板总数事先未知)。

由于某种原因,此代码在引用特定面板的名称(例如“panel2”)时有效,但在我动态引用它(“panelID”)时则无效。

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test',
    defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {

    panel(id:'mainPanel'){
        def panelID 

        (1..6).each {
            panelID = 'panel' + it

            panel(alignmentX: 0f, id: panelID , opaque:true ,background : java.awt.Color.GREEN){
                label('description') 
                textField(id: "description$it", text: panelID, columns: 70 )
                button(id: "button$panelID", text: panelID, actionPerformed : {
                    panelID.background = java.awt.Color.RED
                    panelID.repaint()                       
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                dispose()
            })
        }
    }       
}
frame.pack()
frame.show()

Why does this code fail ?
I want to change the color of one panel in a series of several panels, dynamically constructed (total number of panels not known beforehand).

For some reason, this code works when referencing the name of a particular panel (for example 'panel2'), but not when I refer to it dynamically ('panelID').

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test',
    defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {

    panel(id:'mainPanel'){
        def panelID 

        (1..6).each {
            panelID = 'panel' + it

            panel(alignmentX: 0f, id: panelID , opaque:true ,background : java.awt.Color.GREEN){
                label('description') 
                textField(id: "description$it", text: panelID, columns: 70 )
                button(id: "button$panelID", text: panelID, actionPerformed : {
                    panelID.background = java.awt.Color.RED
                    panelID.repaint()                       
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                dispose()
            })
        }
    }       
}
frame.pack()
frame.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-11-22 22:01:51

要根据元素的 ID 获取元素,您需要将 ID 作为 SwingBuilder 的参数进行访问,如下所示:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    panel(id:'mainPanel'){
        (1..6).each { num ->
            def panelID = "panel$num"
            def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
                label('description') 
                textField(id: "description$num", text:panelID, columns: 70 )
                button(id: "buttonpanel$num", text:panelID, actionPerformed : {
                    swing."$panelID".background = java.awt.Color.RED
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                frame.visible = false
            })
        }
    }       
}

To get the element based on it's ID, you need to access the ID as a parameter of the SwingBuilder, like so:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    panel(id:'mainPanel'){
        (1..6).each { num ->
            def panelID = "panel$num"
            def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
                label('description') 
                textField(id: "description$num", text:panelID, columns: 70 )
                button(id: "buttonpanel$num", text:panelID, actionPerformed : {
                    swing."$panelID".background = java.awt.Color.RED
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                frame.visible = false
            })
        }
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文