gpt4 book ai didi

groovy - 如何在构建面板后动态添加按钮组到面板

转载 作者:行者123 更新时间:2023-12-02 00:14:35 24 4
gpt4 key购买 nike

我正在使用 Groovy Swingbuilder,我想在构建初始面板后动态填充单选按钮 - 按钮组。例子:我有一个有几个选项的面板。根据选择的选项,我需要用一组单选按钮填充按钮组。每个选项的单选选项都不同。

我的面板看起来像这样:

panel(id:"chooseClass", visible:true, layout: new BL()){
vbox(constraints: BL.CENTER){
label("Player Statistics", horizontalAlignment: 0)
label(id: 'raceLabel', text: raceLabelText, horizontalAlignment: 0)
label(id: 'statLabel', text: statLabelText, horizontalAlignment: 0)
panel(id:'classGroupPanel', layout: new GridLayout(1,9)){
myButtonGroup = buttonGroup(id:'classGroup')
}
}
}

然后在我的代码中我有这个方法:

def void setClassGroup(){
def classButtons = plyGen.getAvailibleClass()
// this is one way I've tried it
gPane.edt{panel(id:'classGroupPanel', layout: new GridLayout(1,9)){
buttonGroup(id:'classGroup').with{ group ->
classButtonGroup.each{ radioButton(id: '${it.name}', CreateRadio("${it.name}"), mnemonic:"${it.mnenomic}", buttonGroup: group)}}
}
}
// and this is another way I've tried it
gPane.doOutside {
this.classGroupPanel{
buttonGroup(id:'classGroup').with{group ->
classButtons.each{ gPane.radioButton(id: '${it.name}', CreateRadio("${it.name}"), mnemonic:"${it.mnenomic}", buttonGroup: myButtonGroup) }
}

}
}


}

这两种尝试都可以正确编译和运行,但我没有得到单选按钮列表。我希望有更多关于 swingbuilder 的文档。

最佳答案

缺少 panel.revalidate()?这家伙显示了一个单选按钮组,其中包含一些基于您选择的乐队的歌曲。歌曲呈现为单选按钮:

import groovy.swing.*
import javax.swing.*
import java.awt.*
import java.awt.BorderLayout as BL

def drawSongsPanel(songs) {
def p
new SwingBuilder().edt {
p = panel(visible: true) {
vbox(constraints: BL.CENTER) {
buttonGroup().with { btn ->
songs.each {
radioButton text:it, buttonGroup: btn
}
}
}
}
}
p
}


def getSongs(band) {
switch ( band ) {
case "stones": return ['start me up', 'jumpin jack flash', 'satisfaction']
case "beatles": return ['hey jude', 'yellow submarine', 'yesterday']
}
}


def chooseBand(event, panelBands) {
panelBands.add drawSongsPanel(getSongs(event.actionCommand))
panelBands.revalidate()
}


new SwingBuilder().edt {
frame(defaultCloseOperation: JFrame.EXIT_ON_CLOSE, visible: true, size: [600,500]) {
panel( visible: true, layout: new BL() ) {
vbox(constraints: BL.CENTER){
def panelBands
panelBands = panel(id:'classGroupPanel', layout: new GridLayout(1,9)) {
buttonGroup(id: 'classGroup').with {
radioButton text:"beatles", buttonGroup: it, actionPerformed:{e->chooseBand(e, panelBands)}
radioButton text:"stones", buttonGroup: it, actionPerformed:{e->chooseBand(e, panelBands)}
}
}
}
}
}
}

关于groovy - 如何在构建面板后动态添加按钮组到面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com