ボタンの数と構成が可変のアプリケーションを構築していますが、アプリケーションはボタンのクリックイベントをリッスンして処理する必要があります。
可変数のボタンと構成の要件があるため、スコープ内listenTo
で動的に作成されたボタンを呼び出す必要があると感じています。BoxPanel
しかし、ボタンのイベントは受信されないことがわかりました。
ただし、作成したボタンを覚えていて、listenTo
の範囲外で呼び出すとBoxPanel
、そのイベントが受信されます。
listenTo
BoxPanelのスコープで作業しないのはなぜですか?とにかく、そのような使用法でコンパイラエラーは見られませんでした。
以下は私のサンプルコードです:
import scala.swing._
import scala.swing.event._
object TouchSelectUI extends SimpleSwingApplication {
val touchMap = Vector(
Vector("a", "b", "c"),
Vector("d", "e", "g")
// more may be defined in the future
)
val keyDemension = new Dimension(40, 25)
def top = new MainFrame {
title = "Touch Select Experiment"
val input = new TextArea {
columns = 80
text = ""
}
var aButtonRemebered = new Button()
contents = new FlowPanel {
require(touchMap.size > 0, {println("The touch key definition is empty!")})
for (c <- 0 until touchMap(0).size) {
contents += new BoxPanel(Orientation.Vertical) {
for (r <- 0 until touchMap.size) {
val key = new Button {
text = touchMap(r)(c)
}
contents += key
aButtonRemebered = key
listenTo(key) // listenTo here does not work.
}
}
}
contents += input
}
//listenTo(aButtonRemebered) // Here listTo would work to deliver the event to the following reaction!
reactions += {
case ButtonClicked(b) => {
println(s"Button clicked!")
val c = b.text
input.text = input.text + c
}
}
}
}
あなたの助けをどうもありがとう!
ゆう