我的程序可以正确跟踪球,但是当我切换到记分板时,它不会输出更新的球数。从控制器场景切换回记分牌场景时,如何更新球数。我还知道代码的许多部分仍然缺失,我只是专注于首先获取更新的变量,然后再从那里显示。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Arc;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.Button;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.Bindings;
public class Scoreboard extends Application {
private IntegerProperty Ball = new SimpleIntegerProperty(0);
//int Ball = 0;
int Strike = 0;
int Out = 0;
int[] InningScore = new int[18];
int HomeScore = 0;
int AwayScore = 0;
int Inning = 0;
Scene Controlls, Score;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//Ball add
Button AddBall = new Button("Add Ball");
AddBall.setOnAction(value - > {
AddBall();
});
AddBall.setLayoutX(10);
AddBall.setLayoutY(20);
//Ball Reset
Button ResetBall = new Button("Reset Balls");
ResetBall.setOnAction(value - > {
ResetBall();
});
ResetBall.setLayoutX(10);
ResetBall.setLayoutY(50);
//Strike Add
Button AddStrike = new Button("Add Strike");
AddStrike.setOnAction(value - > {
AddStrike();
});
AddStrike.setLayoutX(110);
AddStrike.setLayoutY(20);
//Strike Reset
Button ResetStrike = new Button("Reset Strikes");
ResetStrike.setOnAction(value - > {
ResetStrike();
});
ResetStrike.setLayoutX(110);
ResetStrike.setLayoutY(50);
//Out Add
Button AddOut = new Button("Add Out");
AddOut.setOnAction(value - > {
AddOut();
});
AddOut.setLayoutX(210);
AddOut.setLayoutY(20);
//Out Reset
Button ResetOut = new Button("Reset Outs");
ResetOut.setOnAction(value - > {
ResetOut();
});
ResetOut.setLayoutX(210);
ResetOut.setLayoutY(50);
//Score Up
Button ScoreUp = new Button("Score Up");
ScoreUp.setOnAction(value - > {
ScoreUp();
});
ScoreUp.setLayoutX(310);
ScoreUp.setLayoutY(20);
//Score Down
Button ScoreDown = new Button("Score Down");
ScoreDown.setOnAction(value - > {
ScoreDown();
});
ScoreDown.setLayoutX(310);
ScoreDown.setLayoutY(50);
//Switch View
Button ShowScoreboard = new Button("Show ScoreBoard");
ShowScoreboard.setOnAction(e - > primaryStage.setScene(Score));
ShowScoreboard.setLayoutX(410);
ShowScoreboard.setLayoutY(20);
Button ShowController = new Button("Show Controller");
ShowController.setOnAction(e - > primaryStage.setScene(Controlls));
ShowController.setLayoutX(410);
ShowController.setLayoutY(40);
//Reset Scoreboard
Button ResetScoreboard = new Button("Reset Scoreboard");
ResetScoreboard.setOnAction(value - > {
ResetScoreboard();
});
ResetScoreboard.setLayoutX(410);
ResetScoreboard.setLayoutY(50);
//Balls Label
Label BallsLabel = new Label();
BallsLabel.setFont(new Font(30));
BallsLabel.setText("Balls");
BallsLabel.setLayoutX(30);
BallsLabel.setLayoutY(20);
//Show Balls
Label ShowBalls = new Label();
ShowBalls.setFont(new Font(30));
ShowBalls.textProperty().bind(new SimpleIntegerProperty(Ball.get()).asString());
ShowBalls.setLayoutX(50);
ShowBalls.setLayoutY(60);
//Strikes Label
Label StrikesLabel = new Label();
StrikesLabel.setFont(new Font(30));
StrikesLabel.setText("Strikes");
StrikesLabel.setLayoutX(120);
StrikesLabel.setLayoutY(20);
//Show Strikes
Label Strikes = new Label();
Strikes.setFont(new Font(30));
Strikes.setText("" + Strike);
Strikes.setLayoutX(150);
Strikes.setLayoutY(60);
//Outs Label
Label OutsLabel = new Label();
OutsLabel.setFont(new Font(30));
OutsLabel.setText("Outs");
OutsLabel.setLayoutX(230);
OutsLabel.setLayoutY(20);
//Show Outs
Label Outs = new Label();
Outs.setFont(new Font(30));
Outs.setText("" + Out);
Outs.setLayoutX(250);
Outs.setLayoutY(60);
//Show Inning Label
Label Inning = new Label();
Inning.setFont(new Font(30));
Inning.setText("Inning");
Inning.setLayoutX(10);
Inning.setLayoutY(140);
//Show Home Label
Label Home = new Label();
Home.setFont(new Font(30));
Home.setText("Home");
Home.setLayoutX(10);
Home.setLayoutY(240);
//Show Away Label
Label Away = new Label();
Away.setFont(new Font(30));
Away.setText("Away");
Away.setLayoutX(10);
Away.setLayoutY(190);
//Show Inning 1 Label
Label Inning1 = new Label();
Inning1.setFont(new Font(30));
Inning1.setText("1");
Inning1.setLayoutX(120);
Inning1.setLayoutY(140);
//Show Inning 2 Label
Label Inning2 = new Label();
Inning2.setFont(new Font(30));
Inning2.setText("2");
Inning2.setLayoutX(170);
Inning2.setLayoutY(140);
//Show Inning 3 Label
Label Inning3 = new Label();
Inning3.setFont(new Font(30));
Inning3.setText("3");
Inning3.setLayoutX(220);
Inning3.setLayoutY(140);
//Show Inning 4 Label
Label Inning4 = new Label();
Inning4.setFont(new Font(30));
Inning4.setText("4");
Inning4.setLayoutX(270);
Inning4.setLayoutY(140);
//Show Inning 5 Label
Label Inning5 = new Label();
Inning5.setFont(new Font(30));
Inning5.setText("5");
Inning5.setLayoutX(320);
Inning5.setLayoutY(140);
//Show Inning 6 Label
Label Inning6 = new Label();
Inning6.setFont(new Font(30));
Inning6.setText("6");
Inning6.setLayoutX(370);
Inning6.setLayoutY(140);
//Show Inning 7 Label
Label Inning7 = new Label();
Inning7.setFont(new Font(30));
Inning7.setText("7");
Inning7.setLayoutX(420);
Inning7.setLayoutY(140);
//Show Inning 8 Label
Label Inning8 = new Label();
Inning8.setFont(new Font(30));
Inning8.setText("8");
Inning8.setLayoutX(470);
Inning8.setLayoutY(140);
//Show Inning 9 Label
Label Inning9 = new Label();
Inning9.setFont(new Font(30));
Inning9.setText("9");
Inning9.setLayoutX(520);
Inning9.setLayoutY(140);
//Show Total
Label Total = new Label();
Total.setFont(new Font(30));
Total.setText("Total");
Total.setLayoutX(570);
Total.setLayoutY(140);
// Create a pane to hold the circle
Pane ControllerView = new Pane();
ControllerView.getChildren().add(AddBall);
ControllerView.getChildren().add(ResetBall);
ControllerView.getChildren().add(AddStrike);
ControllerView.getChildren().add(ResetStrike);
ControllerView.getChildren().add(AddOut);
ControllerView.getChildren().add(ResetOut);
ControllerView.getChildren().add(ScoreUp);
ControllerView.getChildren().add(ScoreDown);
ControllerView.getChildren().add(ShowScoreboard);
ControllerView.getChildren().add(ResetScoreboard);
Pane ScoreboardView = new Pane();
ScoreboardView.getChildren().add(ShowController);
ScoreboardView.getChildren().add(ShowBalls);
ScoreboardView.getChildren().add(Strikes);
ScoreboardView.getChildren().add(Outs);
ScoreboardView.getChildren().add(Inning);
ScoreboardView.getChildren().add(BallsLabel);
ScoreboardView.getChildren().add(StrikesLabel);
ScoreboardView.getChildren().add(OutsLabel);
ScoreboardView.getChildren().add(Inning1);
ScoreboardView.getChildren().add(Inning2);
ScoreboardView.getChildren().add(Inning3);
ScoreboardView.getChildren().add(Inning4);
ScoreboardView.getChildren().add(Inning5);
ScoreboardView.getChildren().add(Inning6);
ScoreboardView.getChildren().add(Inning7);
ScoreboardView.getChildren().add(Inning8);
ScoreboardView.getChildren().add(Inning9);
ScoreboardView.getChildren().add(Home);
ScoreboardView.getChildren().add(Away);
ScoreboardView.getChildren().add(Total);
// Create a scene and place it in the stage
Controlls = new Scene(ControllerView, 600, 200, Color.GREEN);
Score = new Scene(ScoreboardView, 700, 320, Color.GREEN);
primaryStage.setTitle("ScoreBoard"); // Set the stage title
primaryStage.setScene(Controlls); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public void AddBall() {
Ball.set(Ball.get() + 1);
if (Ball.get() == 4) {
Ball.set(0);
Strike = 0;
}
System.out.println(Ball.get());
}
public void ResetBall() {
Ball.set(0);
}
public void AddStrike() {
Strike++;
if (Strike == 3) {
Out++;
Strike = 0;
Ball.set(0);
}
}
public void ResetStrike() {
Strike = 0;
}
public void AddOut() {
Out++;
Strike = 0;
Ball.set(0);
if (Out == 3) {
Inning++;
}
}
public void ResetOut() {
Out = 0;
}
public void ScoreUp() {
InningScore[Inning]++;
}
public void ScoreDown() {
InningScore[Inning]--;
}
public void ResetScoreboard() {
Ball.set(0);
Strike = 0;
Out = 0;
for (int i = 0; i < 18; i++) {
InningScore[i] = 0;
}
}
public static void main(String[] args) {
launch(args);
}
}
我只希望它显示当我切换回记分板场景时有多少个球
答案 0 :(得分:1)
从
更改此行ShowBalls.textProperty().bind(new SimpleIntegerProperty(Ball.get()).asString());
对此
ShowBalls.textProperty().bind(Ball.asString());
作为旁注,我知道您仍在从事此工作,但由于所有功能都在同一类中,因此它们都应该是私有的而不是公共的,并且传统上,功能以小写首字母开头,例如您拥有
public void ResetScoreboard() {
应该是
private void resetScoreboard() {
变量名Ball
应该是ball
相同我只尝试为类名保留大写的起始字母,如果有机会我会给Java命名约定一个google,它可以帮助所有人阅读其他代码时位于同一页面。
祝您项目顺利,希望一切顺利