Javafx GUI记分牌

时间:2017-11-22 01:15:30

标签: java javafx

我正在尝试在JavaFX中创建一个基本的GUI记分板,但是在程序中遇到错误。

package application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Counter extends Application {
    public int P1_Score = 0;
    public int P2_Score = 0;

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

    @Override
    public void start(Stage primaryStage) {

        // vertical box
        VBox vertBox = new VBox();
        vertBox.setPrefSize(400, 400);
        vertBox.setAlignment(Pos.CENTER);

        // horizontal box for the players
        HBox PlayerName = new HBox();
        PlayerName.setAlignment(Pos.CENTER);
        PlayerName.setPadding(new Insets(25, 25, 25, 25));

        // horizontal box for the scores
        HBox Score = new HBox();
        Score.setAlignment(Pos.CENTER);

        // horizontal box to show a goal
        HBox Goal = new HBox();
        Goal.setAlignment(Pos.CENTER);

        // player1 name
        Label team1 = new Label("Player 1");
        team1.setTextFill(Color.RED);
        team1.setAlignment(Pos.TOP_LEFT);
        team1.setPadding(new Insets(25, 25, 25, 25));

        // player2 name
        Label team2 = new Label("Player 2");
        team2.setTextFill(Color.BLUE);
        team2.setAlignment(Pos.TOP_RIGHT);
        team2.setPadding(new Insets(25, 25, 25, 25));

        // player1 score
        Label text1 = new Label();
        text1.setAlignment(Pos.CENTER_LEFT);
        text1.setText(Integer.toString(P1_Score));
        text1.setPadding(new Insets(25, 30, 50, 25));

        // player2 score
        Label text2 = new Label();
        text2.setAlignment(Pos.CENTER_RIGHT);
        text2.setText(Integer.toString(P2_Score));
        text2.setPadding(new Insets(25, 30, 50, 25));

        // player1 goal
        Button btn1 = new Button("Player 1 score");
        btn1.setAlignment(Pos.BOTTOM_LEFT);
        btn1.setPadding(new Insets(10, 10, 10, 10));

        // player2 goal
        Button btn2 = new Button("Player 2 score");
        btn2.setPadding(new Insets(10, 10, 10, 10));
        btn2.setAlignment(Pos.BOTTOM_RIGHT);

        // event for player1 goal
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                // increase score
                P1_Score = P1_Score + 1;
                // display player1 score
                text1.setText(Integer.toString(P1_Score));
                if (P1_Score == 3) {
                    resetScore(text1, text2);
                }

            }

        });

        // event for player2 goal
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                // increase player2 score
                P2_Score = P2_Score + 1;
                // display score
                text2.setText(Integer.toString(P2_Score));

                if (P2_Score == 3) {
                    resetScore(text1, text2);

                }
            }


        }); 

        PlayerName.getChildren().addAll(team1, team2);
        Score.getChildren().addAll(text1, text2);
        Goal.getChildren().addAll(btn1, btn2);

        vertBox.getChildren().addAll(PlayerName, Score, Goal);
        primaryStage.setTitle("Counter");

        Scene scene = new Scene(vertBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void resetScore(Label text1, Label text2) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        P1_Score = 0;
        P2_Score = 0;
        text1.setText(Integer.toString(P1_Score));
        text2.setText(Integer.toString(P2_Score));


    }
}

代码应该是基本记分牌的模拟。当任一球员得分三球时,记分牌模拟应显示最终得分两秒,然后重置为0-0。

目前,当分数等于3时,程序会冻结,但分数不会更新。如果得分为2-1,则玩家1有两个并且他们获得第三个目标,屏幕冻结2-1并重置。如何让分数显示第三个目标,然后重置。谢谢。

1 个答案:

答案 0 :(得分:1)

您不应使用Thread.Sleep();阻止JavaFx应用程序线程(与swing相同),因此请使用javafx.concurrent.Task来实现目标

请参阅下面的更新代码,

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class JavaFXApplication4 extends Application {

    public int P1_Score = 0;
    public int P2_Score = 0;

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

    @Override
    public void start(Stage primaryStage) {

        // vertical box
        VBox vertBox = new VBox();
        vertBox.setPrefSize(400, 400);
        vertBox.setAlignment(Pos.CENTER);

        // horizontal box for the players
        HBox PlayerName = new HBox();
        PlayerName.setAlignment(Pos.CENTER);
        PlayerName.setPadding(new Insets(25, 25, 25, 25));

        // horizontal box for the scores
        HBox Score = new HBox();
        Score.setAlignment(Pos.CENTER);

        // horizontal box to show a goal
        HBox Goal = new HBox();
        Goal.setAlignment(Pos.CENTER);

        // player1 name
        Label team1 = new Label("Player 1");
        team1.setTextFill(Color.RED);
        team1.setAlignment(Pos.TOP_LEFT);
        team1.setPadding(new Insets(25, 25, 25, 25));

        // player2 name
        Label team2 = new Label("Player 2");
        team2.setTextFill(Color.BLUE);
        team2.setAlignment(Pos.TOP_RIGHT);
        team2.setPadding(new Insets(25, 25, 25, 25));

        // player1 score
        Label text1 = new Label();
        text1.setAlignment(Pos.CENTER_LEFT);
        text1.setText(Integer.toString(P1_Score));
        text1.setPadding(new Insets(25, 30, 50, 25));

        // player2 score
        Label text2 = new Label();
        text2.setAlignment(Pos.CENTER_RIGHT);
        text2.setText(Integer.toString(P2_Score));
        text2.setPadding(new Insets(25, 30, 50, 25));

        // player1 goal
        Button btn1 = new Button("Player 1 score");
        btn1.setAlignment(Pos.BOTTOM_LEFT);
        btn1.setPadding(new Insets(10, 10, 10, 10));

        // player2 goal
        Button btn2 = new Button("Player 2 score");
        btn2.setPadding(new Insets(10, 10, 10, 10));
        btn2.setAlignment(Pos.BOTTOM_RIGHT);

        // event for player1 goal
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                if (P1_Score >= 3) { // to avoid futher Score update after 3
                    return;
                }
                // increase score
                P1_Score = P1_Score + 1;
                // display player1 score
                text1.setText(Integer.toString(P1_Score));
                if (P1_Score == 3) {
                    resetScore(text1, text2);
                }
            }

        });

        // event for player2 goal
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {

                if (P2_Score >= 3) { // to avoid futher Score update after 3
                    return;
                }
                // increase player2 score
                P2_Score = P2_Score + 1;
                // display score
                text2.setText(Integer.toString(P2_Score));

                if (P2_Score == 3) {
                    resetScore(text1, text2);
                }
            }

        });

        PlayerName.getChildren().addAll(team1, team2);
        Score.getChildren().addAll(text1, text2);
        Goal.getChildren().addAll(btn1, btn2);

        vertBox.getChildren().addAll(PlayerName, Score, Goal);
        primaryStage.setTitle("Counter");

        Scene scene = new Scene(vertBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void resetScore(Label text1, Label text2) {

        Task task = new Task() {
            @Override
            public Void call() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };

        task.setOnSucceeded((e) -> {
            P1_Score = 0;
            P2_Score = 0;
            text1.setText(Integer.toString(P1_Score));
            text2.setText(Integer.toString(P2_Score));
        });
        new Thread(task).start();

    }

}

P.S 您可以使用javafx.animation.PauseTransition

实现相同的目标
private void resetScore(Label text1, Label text2) {

    PauseTransition pause = new PauseTransition(Duration.millis(2000));
    pause.setOnFinished(e -> {
        P1_Score = 0;
        P2_Score = 0;
        text1.setText(Integer.toString(P1_Score));
        text2.setText(Integer.toString(P2_Score));
    });
    pause.play();

}