2017-11-22 14 views
2

JavaFXで基本的なGUIスコアボードを作成しようとしていますが、プログラム内でエラーが発生しています。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)); 


    } 
} 

コードは、基本スコアボードのシミュレーションであるはずです。いずれかのプレイヤーが3ゴールを得た場合、スコアボードシミュレーションは2秒間の最終スコアを表示し、0-0にリセットする必要があります。

現在、スコアが3に等しいが、スコアが更新されないと、プログラムはフリーズします。スコアが2-1の場合、プレイヤー1は2つで3つ目のゴールが得られ、2-1でフリーズしてリセットされます。スコアを3番目のゴールにしてリセットするにはどうしたらいいですか?ありがとうございました。

+0

https://stackoverflow.com/questions/35369591/javafx https://stackoverflow.com/questions/35749922/unexpected-behaviour-with-thread-sleepの前に継続する前に強制的に更新しています –

答えて

1

あなたは

は、以下の更新されたコード、

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(); 

    } 

} 

することができますPS同じ目標を参照してくださいあなたの目標を達成するためにjavafx.concurrent.Taskを使用するため、(スイングと同じ)Thread.Sleep();を使用してJavaFXのアプリケーションスレッドをブロックしてはいけません達成するために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(); 

} 
関連する問題