У меня есть программа, которая берет 2 имени игрока с экрана приветствия, а затем предполагается использовать их в игре в кости, однако кажется, что они не сохраняются.
Когда я запускаюпрограмма Я получаю вывод, подобный этому
after welcome button click player1 = 4
after welcome button click player2 = 3
player1 = null
player2 = null
Я хотел бы быть что-то вроде
after welcome button click player1 = 4
after welcome button click player2 = 3
player1 = 4
player2 = 3
Вот соответствующий код
Game.java
package DiceGame;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
public class Game extends Application {
Parent root, welcomeScreen;
Stage app = new Stage();
Stage welcome = new Stage();
Player player1 = new Player();
Player player2 = new Player();
private boolean player1Turn = true;
@FXML
private TextField player1Name;
@FXML
private TextField player2Name;
@FXML
private Button closeButton;
@FXML
private Button rollButton;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// Load game board
root = FXMLLoader.load(getClass().getResource("Game.fxml"));
// Load the welcome screen
welcomeScreen = FXMLLoader.load(getClass().getResource("WelcomeScreen.fxml"));
// Set the game board on the window
app.setScene(new Scene(root, 742, 338));
app.show();
// Set the welcome screen to the window, display above the game board
welcome.setScene(new Scene(welcomeScreen, 600, 400));
welcome.show();
}
private Player getPlayer1() {
return this.player1;
}
private Player getPlayer2() {
return this.player2;
}
@FXML
private void closeWelcomeScreen() {
// Get the stage that the button is attached to it
this.player1.setName(player1Name.getText());
this.player2.setName(player2Name.getText());
System.out.println("after welcome button click player1 = " + player1.getName());
System.out.println("after welcome button click player2 = " + player2.getName());
Stage stage = (Stage) closeButton.getScene().getWindow();
// Close the welcome screen
stage.hide();
}
@FXML
private void doRoll() {
// Perform turn
// Update text for button
System.out.println("player1 = " + player1.getName());
System.out.println("player2 = " + player2.getName());
}
}
WelcomeScreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="DiceGame.Game">
<children>
<Label layoutX="201.0" prefHeight="73.0" prefWidth="244.0" text="Dice Game">
<font>
<Font size="41.0" />
</font>
</Label>
<TextArea layoutX="1.0" layoutY="64.0" prefHeight="250.0" prefWidth="600.0" text="Welcome to my dice game! The rules are simple • You will play a computer • You will take turns rolling dice, until one, or both, players score over 21 points • The points are as follows a) If a pair is rolled, the sum of the pair will be added to the score b) If all 3 dice have the same values, 18 will be added to the score c) Otherwise 1 point will be added tot he score " />
<TextField fx:id="player1Name" layoutX="150.0" layoutY="325.0" prefHeight="15.0" prefWidth="75.0" />
<Label layoutX="50.0" layoutY="325.0" text="Player 1 Name: ">
<padding>
<Insets top="3.0" />
</padding>
</Label>
<Label layoutX="275.0" layoutY="325.0" text="Player 2 Name: ">
<padding>
<Insets top="3.0" />
</padding>
</Label>
<TextField fx:id="player2Name" layoutX="375.0" layoutY="325.0" prefHeight="15.0" prefWidth="75.0" />
<Button fx:id="closeButton" layoutX="278.0" layoutY="361.0" mnemonicParsing="false" onAction="#closeWelcomeScreen" text="Close" />
</children>
</AnchorPane>
Game.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane prefHeight="700.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="DiceGame.Game">
<top>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Label fx:id="player1ScoreLabel" text="Player Score: 0">
<padding>
<Insets bottom="15.0" left="10.0" right="10.0" top="10.0" />
</padding></Label>
<Label fx:id="player2ScoreLabel" text="Computer Score: 0">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding></Label>
<Label fx:id="leadLabel" text="Both players tied">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</Label>
</children>
</HBox>
</top>
<bottom>
<Button fx:id="rollButton" mnemonicParsing="false" onAction="#doRoll" text="Click for your turn" BorderPane.alignment="CENTER" />
</bottom>
<center>
</center>
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fx:id="firstDie" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@side_1.png" />
</image>
<GridPane.margin>
<Insets left="45.0" />
</GridPane.margin></ImageView>
<ImageView fx:id="secondDie" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1">
<image>
<Image url="@side_1.png" />
</image>
<GridPane.margin>
<Insets left="45.0" />
</GridPane.margin></ImageView>
<ImageView fx:id="thirdDie" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2">
<image>
<Image url="@side_1.png" />
</image>
<GridPane.margin>
<Insets left="45.0" />
</GridPane.margin></ImageView>
</children>
</GridPane>
</center>
</BorderPane>
Как получить желаемый результат?
Спасибо, Джон