我想在 ObservableBooleanValue 的 when() 条件中添加第二个参数。如果只有一个参数 它工作正常。该行的问题:
game.winnerProperty().isEqualTo(Square.State.EMPTY) || (GameTimer::isTimeOver==true)
没关系:
game.winnerProperty().isEqualTo(Square.State.EMPTY) //This is BooleanBinding
代码:
playerLabel.textProperty().bind(
Bindings.when(
game.gameOverProperty().not()
)
.then("Actual Player: ")
.otherwise(
Bindings.when(
game.winnerProperty().isEqualTo(Square.State.EMPTY) || (GameTimer::isTimeOver==true)
)
.then("Draw")
.otherwise("Winner: ")
)
);
如何添加第二个 boolean 类型的参数?
最佳答案
有时组合多个绑定(bind)很方便。
然而,这会导致难以理解/维护的复杂代码。使用 Bindings.createStringBinding
并添加适当的依赖项会更容易:
playerLabel.textProperty().bind(
Bindings.createStringBinding(() -> {
if (game.isGameOver()) {
return Square.State.EMPTY.equals(game.getWinner()) || gameTimer.isTimeOver()
? "Draw"
: "Winner: ";
} else {
return "Actual Player: ";
}
},
game.gameOverProperty(),
game.winnerProperty(),
gameTimer.timeOverProperty()));
关于java - 如何在 Java 中使用两个参数创建 ObservableBooleanValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44142780/