根据布尔JavaFX更改标签

时间:2017-05-11 08:46:25

标签: java javafx data-binding label

我是JavaFX的新手,而且对于我的跳棋游戏,我想要显示谁在我的棋盘上方。我已经阅读了多个非常相似的问题,但它似乎并没有让我想到我想去的地方。我希望你能帮助我!

我有一个包含以下代码的FXML文件:

<HBox fx:id="topBox" alignment="TOP_CENTER" prefHeight="45.0" prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
     <children>
        <Label fx:id="labelTurn" text="test" />
        <Button fx:id="buttonResign" alignment="CENTER" mnemonicParsing="false" onAction="#doResign" prefHeight="25.0" prefWidth="80.0" text="Resign" />
     </children>
  </HBox>

在我的主要:

public class Main extends Application {

    Checkers content = new Checkers();
    BorderPane border = new BorderPane(); 

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            border.setTop(FXMLLoader.load(getClass().getResource("Sample.fxml")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        border.setCenter(createContent());
        Scene scene = new Scene(border);
        primaryStage.setTitle("Checkers from main");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的控制器:

public class SampleController {
    @FXML
    private Label labelTurn;

    @FXML
    private HBox topBox;

    @FXML
    private Button buttonResign;

    private Checkers content = new Checkers();

    @FXML
    void doResign(ActionEvent event) {
        content.resign();
        // System.exit(0); //TODO eine tatsächliche Methode eingeben!!

    }

    public void setLabelTurn(Label labelTurn) {
        this.labelTurn = labelTurn;

        String setLabelText() {
        String s = " ";
        if (content.getWhiteTurnPropValue()) {
            labelTurn.setText("White, it is your turn.");
            s = "White, it is your turn.";
            return s;
        } else {
            labelTurn.setText("Black, it is your turn.");
            s = "Black, it is your turn.";
            return s;
        }
    }
    @FXML
    void initialize() {
        assert buttonResign != null : "fx:id=\"buttonResign\" was not injected: check your FXML file 'Sample.fxml'.";
        assert topBox != null : "fx:id=\"topBox\" was not injected: check your FXML file 'Sample.fxml'.";
        assert labelTurn != null : "fx:id=\"labelTurn\" was not injected: check your FXML file 'Sample.fxml'.";
        labelTurn.textProperty().setValue(setLabelText());
    }
}

我的游戏数据有一个BooleanProperty来显示它是谁。每次成功移动后都会发生变化。

BooleanProperty whiteTurnProp = new SimpleBooleanProperty(true);

public BooleanProperty getWhiteTurnProp() {
    return whiteTurnProp;
}

public void setWhiteTurnPropValue(Boolean whiteTurn) {
    this.whiteTurnProp.setValue(whiteTurn);
}

public boolean getWhiteTurnPropValue() {
    return this.whiteTurnProp.getValue();
}

据我所知,我需要将Label绑定到我在initialize方法中完成的值。但是当我玩游戏并改变游戏类中的值时,标签保持不变。如何更新标签?我想我需要添加一个更改监听器,但我不知道该怎么做,更具体地说是在哪里。

非常感谢你的帮助!我缩短了我的代码并希望它还不会太长。

最佳, 丽莎

1 个答案:

答案 0 :(得分:1)

initialize中:labelTurn.textProperty().setValue(setLabelText());不是绑定,它是属性上的一个简单集。一个非常奇怪的问题setLabelText()设置Label的文本,并返回已设置的字符串,该值再次用于Label的文本。

您可以创建一个真正的绑定:

labelTurn.textProperty().bind(Bindings.createStringBinding(() -> {
    String s = " ";
    if (content.getWhiteTurnProp().get())
        s = "White, it is your turn.";
     else
        s = "Black, it is your turn.";

    return s;
}, content.getWhiteTurnProp()));