如果您将 numPanels 作为固定值,例如 20,那么这确实是一个很好的和时髦的摆动。
但是我从两天开始尝试动态添加或多或少的带有 groovy.swing.Spinner 的面板(我的程序将用于连接到不同的数据库并交换来自不同数据库表的值)。我尝试了绑定、重新验证、重新绘制 - 但我不能带来闭包以从微调器中获取新值......
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import javax.swing.SpinnerNumberModel
int numPanels = 2
swing = new SwingBuilder()
def setPanelAmount = swing.action(name:'Amount of Panels in vbox-element', closure: this.&setPanelAmount )
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.HIDE_ON_CLOSE) {
panel(id:'mainPanel'){
scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) {
vbox {
(1..numPanels).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){
hbox(){
label 'Change amount of panels:'
hstrut(10)
spinner(id: 'numPanelSpinner', stateChanged: this.&setPanelAmount, model: new SpinnerNumberModel(2, 2, 10, 1))
hstrut(50)
button('Quit', actionPerformed:{
frame.visible = false
})
}
}
}
}
frame.size = [ frame.width, 600 ]
def setPanelAmount(event) {
numPanels = swing.numPanelSpinner.getValue()
}