下面是一些示例代码,使用 Groovy 的 swingbuilder 为 JList 的 valueChanged 事件创建代码:
mainList.valueChanged = { event ->
if (event.isAdjusting) {
index = mainList.selectedIndex
otherList.clearSelection()
otherIndex = otherList.selectedIndex
} else {
mainListSelected = true
clearJList(otherList)
}
}
我有两个 JList,这种函数控制允许通过 mainListSelected 变量选择哪个列表。然后,我们还必须根据它是来自 mainList 还是 otherList 的索引,从选择中更改我们想要使用的 int eindex
我已经阅读了有关 event.isAdjusting 的信息,它只会在鼠标单击事件中像这样触发两次。有了这些知识,您可能会认为我会将所有内容移出那里,但如果鼠标是导致事件的原因而不是使用箭头,我需要以不同的方式发生某些事情。但是,使用此代码,使用箭头键导航可以防止索引发生变化。