单击按钮后变量不持久

时间:2018-11-28 21:20:28

标签: java javafx

我有一个程序,它从欢迎屏幕中获取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!&#10;&#10;The rules are simple&#10;&#10;• You will play a computer&#10;• You will take turns rolling dice, until one, or both, players score over 21 points&#10;• The points are as follows&#10;    a) If a pair is rolled, the sum of the pair will be added to the score&#10;    b) If all 3 dice have the same values, 18 will be added to the score&#10;    c) Otherwise 1 point will be added tot he score &#10;&#10;&#10;&#10;" />
      <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>

我如何获得理想的结果?

谢谢 约翰

1 个答案:

答案 0 :(得分:1)

代码中的问题是每个fxml都有其专用的控制器实例(即使您使用相同的类)。因此,您要在WelcomeScreen控制器实例中设置详细信息,并希望在Game控制器实例中获取它们。

首先,我建议为每个fxml使用专用的控制器类。另外,最好不要将Application类用作控制器类。就您而言,您可以将玩家名称保存在Application类中,并可以从其他控制器访问它们。

另一种快速的怪异方法是将您的播放器声明为静态;)(绝对不推荐)