如何在视图中正确更新 IntegerProperty 的值?
How to correctly update value of IntegerProperty in the view?
我正在构建一个简单的应用程序,它将跟踪 2 位玩家的某个桌面游戏。
我有一个名为 MatchView
的视图
class MatchView : View() {
// data
private var currentRound = SimpleIntegerProperty(0)
private var currentTurn = SimpleIntegerProperty(0)
override val root = borderpane {
center = label(currentRound.stringBinding{ "Round %d".format(it) })
// other layout-related stuff
subscribe<EndTurnEvent> {
endPlayerTurn()
}
}
private fun endPlayerTurn() {
// increment turn
currentTurn.plus(1)
currentRound.plus(currentTurn.value % 2)
}
}
订阅了 EndTurnEvent
- 视图使用的片段之一发出的事件。
被调用的方法应该增加 currentTurn
的值,如果需要 currentRound
(第二个玩家结束回合后的回合增量)
然而,当我调用 .plus()
方法时,currentRound
和 currentTurn
的值都没有增加。
我尝试过以不同的方式编辑值:
private fun endPlayerTurn() {
// increment turn
currentTurn.value = currentTurn.value + 1
currentRound.value = currentTurn.value % 2
}
但这让我感到震惊java.lang.IllegalStateException: Not on FX application thread
我知道将属性放入视图是反模式,但由于我只想跟踪 2 个属性,我认为我可以将它们直接放入 View
Platform.runLater(() -> {
// Update GUI from another Thread here
});
我正在构建一个简单的应用程序,它将跟踪 2 位玩家的某个桌面游戏。
我有一个名为 MatchView
class MatchView : View() {
// data
private var currentRound = SimpleIntegerProperty(0)
private var currentTurn = SimpleIntegerProperty(0)
override val root = borderpane {
center = label(currentRound.stringBinding{ "Round %d".format(it) })
// other layout-related stuff
subscribe<EndTurnEvent> {
endPlayerTurn()
}
}
private fun endPlayerTurn() {
// increment turn
currentTurn.plus(1)
currentRound.plus(currentTurn.value % 2)
}
}
订阅了 EndTurnEvent
- 视图使用的片段之一发出的事件。
被调用的方法应该增加 currentTurn
的值,如果需要 currentRound
(第二个玩家结束回合后的回合增量)
然而,当我调用 .plus()
方法时,currentRound
和 currentTurn
的值都没有增加。
我尝试过以不同的方式编辑值:
private fun endPlayerTurn() {
// increment turn
currentTurn.value = currentTurn.value + 1
currentRound.value = currentTurn.value % 2
}
但这让我感到震惊java.lang.IllegalStateException: Not on FX application thread
我知道将属性放入视图是反模式,但由于我只想跟踪 2 个属性,我认为我可以将它们直接放入 View
Platform.runLater(() -> {
// Update GUI from another Thread here
});