【发布时间】:2020-01-29 06:10:45
【问题描述】:
我正在学习 Kotlin 和 TornadoFX 来编写一个非常简单的任务:两个按钮,一个带有 Int 1,另一个带有 Int 200。当我单击按钮一时,我想更新并显示带有 Int 2 的按钮一和其他与 Int 201。以下程序将允许我检查更新数据,并且一切似乎都是正确的。我的问题是为什么按钮上的显示不会相应更新。 请帮忙。
class MainView : View("Hello TornadoFX") {
val controllera: ControllerA by inject()
val i: Int? = 1
val j: Int? = 2
override val root = hbox {
button(controllera.s1.toString()) {
setOnAction {
setId(i.toString())
println(" in view button 1 s1: { ${controllera.s1.toString()} }")
controllera.temp(i)
}
}
button(controllera.s2.toString()) {
setOnAction {
setId(j.toString())
println(" in view button 2 s2: { ${controllera.s2.toString()} }")
controllera.temp(j)
}
}
label(title) {
addClass(Styles.heading)
}
}
}
class ControllerA :Controller(){
var s1: Int =1
var s2: Int = 200
fun temp(k: Int?) {
when( k) {
1 -> {println("botton ID " +k)
println(" current value of s1: $s1 ")
s1 = s1+1
println(" new value of s1 to update: $s1 ")
s2 = s2 +1
println(" new value of s2 to update: $s2 ")}
2 -> {
println("botton ID " + k)
println("current value of s2: $s2 ")}
else -> println("default")
}
}
}
test output at various location:
in view button 1 s1: { 1 } //In MainView
botton ID 1 //in ControllerA
s1: 1
new value of s1 to update: 2
new value of s2 to update: 201
in view button 2 s2: { 201 } //in MainView
botton ID 2 //in ControllerA
current value of s2: 201
Note: those updated data will not be display on button text.
【问题讨论】: