在 JavaFX 中更新对象状态更改的 UI 节点的正确方法是什么?

有一个名为播放器的java类,如下所示:


public class Player {

    private String playerName;

    private int score;


    public Player(String playerName) {

        this.playerName = playerName;

        this.score = 0;

    }


    public void incrementScore(int by) {

        this.score+=by;

    }


    public String getPlayerName() {

        return this.playerName;

    }


}

我只是想知道,一旦 Player 实例更改其分数,哪种正确方法可以更新 UI,例如提供的 UI:


public class PlayerUI extends Application {


    public static void main(String[] args) {

        launch();

    }


    @Override

    public void start(Stage primaryStage) throws Exception {

        Player player = new Player("Mario");


        VBox box = new VBox();

        box.setSpacing(20.);

        box.setPadding(new Insets(20.));


        Text playerName = new Text("Player: " + player.getPlayerName());

        Text score = new Text("Score: " + player.getPlayerScore());


        Button incrementScore = new Button("Score up");

        incrementScore.setOnAction(new IncrementScoreHandler(player, score));


        box.getChildren().add(playerName);

        box.getChildren().add(score);

        box.getChildren().add(incrementScore);


        Scene mainScene = new Scene(box);

        primaryStage.setScene(mainScene);

        primaryStage.setResizable(false);


        primaryStage.show();

    }


}

我使用事件处理程序执行此操作,该处理程序将播放器和所涉及的节点作为构造函数中的参数接收。


public class IncrementScoreHandler implements EventHandler<ActionEvent> {

    private Player player;

    private Text scoreText;


    public IncrementScoreHandler(Player player, Text score) {

        this.player = player;

        this.scoreText = score;

    }


    @Override

    public void handle(ActionEvent event) {

        player.incrementScore(1);

        this.scoreText.setText("Score: " + player.getPlayerScore());


    }


}

它是正确的吗?重构、让和得分更好吗?PlayerplayerNameStringPropertyIntegerProperty


catspeake
浏览 95回答 1
1回答

开心每一天1111

是的,您可能希望重构类以改用可观察属性。Player然后,只需将节点(或以下示例中的节点)绑定到这些属性即可。TextLabelimport javafx.application.Application;import javafx.beans.property.IntegerProperty;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.geometry.Insets;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.layout.GridPane;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class PropertiesExample extends Application {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; &nbsp; &nbsp; // Simple interface&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(5);&nbsp; &nbsp; &nbsp; &nbsp; root.setPadding(new Insets(10));&nbsp; &nbsp; &nbsp; &nbsp; root.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; // Create our Player&nbsp; &nbsp; &nbsp; &nbsp; Player player = new Player("Mario");&nbsp; &nbsp; &nbsp; &nbsp; // For this example, a simple GridPane will hold our nodes&nbsp; &nbsp; &nbsp; &nbsp; GridPane gridPane = new GridPane();&nbsp; &nbsp; &nbsp; &nbsp; // Add our data headers&nbsp; &nbsp; &nbsp; &nbsp; gridPane.add(new Label("Player:"), 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; gridPane.add(new Label("Score:"), 0, 1);&nbsp; &nbsp; &nbsp; &nbsp; // Our labels to hold the changeable data&nbsp; &nbsp; &nbsp; &nbsp; Label lblPlayerName = new Label();&nbsp; &nbsp; &nbsp; &nbsp; Label lblPlayerScore = new Label();&nbsp; &nbsp; &nbsp; &nbsp; gridPane.add(lblPlayerName, 1, 0);&nbsp; &nbsp; &nbsp; &nbsp; gridPane.add(lblPlayerScore, 1, 1);&nbsp; &nbsp; &nbsp; &nbsp; // Use the Player properties to bind our displayed values to the Labels&nbsp; &nbsp; &nbsp; &nbsp; lblPlayerName.textProperty().bind(player.playerNameProperty());&nbsp; &nbsp; &nbsp; &nbsp; lblPlayerScore.textProperty().bind(player.scoreProperty().asString());&nbsp; &nbsp; &nbsp; &nbsp; Button btnIncrementScore = new Button("Score Up");&nbsp; &nbsp; &nbsp; &nbsp; btnIncrementScore.setOnAction(event -> player.incrementScore());&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().addAll(gridPane, btnIncrementScore);&nbsp; &nbsp; &nbsp; &nbsp; // Show the Stage&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setWidth(300);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setHeight(300);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java