为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

catspeake 2022-09-21 17:36:03

有一个名为播放器的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


查看完整描述

1 回答

?
HappyDay1234567890

TA贡献1508条经验 获得超11个赞

是的,您可能希望重构类以改用可观察属性。Player


然后,只需将节点(或以下示例中的节点)绑定到这些属性即可。TextLabel


import 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 {


    public static void main(String[] args) {

        launch(args);

    }


    @Override

    public void start(Stage primaryStage) {


        // Simple interface

        VBox root = new VBox(5);

        root.setPadding(new Insets(10));

        root.setAlignment(Pos.CENTER);


        // Create our Player

        Player player = new Player("Mario");


        // For this example, a simple GridPane will hold our nodes

        GridPane gridPane = new GridPane();


        // Add our data headers

        gridPane.add(new Label("Player:"), 0, 0);

        gridPane.add(new Label("Score:"), 0, 1);


        // Our labels to hold the changeable data

        Label lblPlayerName = new Label();

        Label lblPlayerScore = new Label();

        gridPane.add(lblPlayerName, 1, 0);

        gridPane.add(lblPlayerScore, 1, 1);


        // Use the Player properties to bind our displayed values to the Labels

        lblPlayerName.textProperty().bind(player.playerNameProperty());

        lblPlayerScore.textProperty().bind(player.scoreProperty().asString());


        Button btnIncrementScore = new Button("Score Up");

        btnIncrementScore.setOnAction(event -> player.incrementScore());


        root.getChildren().addAll(gridPane, btnIncrementScore);


        // Show the Stage

        primaryStage.setWidth(300);

        primaryStage.setHeight(300);

        primaryStage.setScene(new Scene(root));

        primaryStage.show();

    }

}


查看完整回答
反对 回复 2022-09-21

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信