java - 如何在 Java 中使用两个参数创建 ObservableBooleanValue?

标签 java javafx java-8 boolean-logic

我想在 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/

相关文章:

java - spring boot/jpa 无法使用用户名 "@localhost"使用密码 no 连接到 mysql?

java - Double.isFinite 实现细节 - 为什么是 DoubleConsts.MAX_VALUE 而不是 Double.MAX_VALUE?

Java 8 Streams 分组收集器

scala持续时间: "This class is not meant as a general purpose representation of time, it is optimized for the needs of scala.concurrent."

java - Swing set布局Bug

java - 无法读取名称为 : [xx] in namespace ['default' ] Ignoring 的配置映射

java - 检查EntityManagerFactory

javafx:如何格式化双重属性以进行绑定(bind)?

java - 使用属性绑定(bind)Javafx获取圆心

java - 有没有办法在 Controller 中初始化变量并在 FXML 中使用它?