gpt4 book ai didi

java - 单击按钮后变量不持久

转载 作者:行者123 更新时间:2023-11-30 10:08:57 24 4
gpt4 key购买 nike

我有一个程序从欢迎屏幕中获取 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());

}
}

欢迎屏幕.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>

游戏.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>

我怎样才能得到想要的结果?

谢谢约翰

最佳答案

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

首先,我建议为每个 fxml 设置专用的 Controller 类。此外,最好不要将 Application 类用作 Controller 类。在您的情况下,您可以将玩家名称保存在应用程序类中,并可以从不同的 Controller 访问它们。

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

关于java - 单击按钮后变量不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53528281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com