- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序从欢迎屏幕中获取 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! 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>
游戏.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/
所以我有这个 javascript 片段,它有两个按钮可以进入全屏,一个按钮可以退出全屏。我想做到这一点,如果我不在全屏模式下,按钮会显示转到全屏模式,而当我处于全屏模式时,按钮会显示退出全屏模式..
我在自定义控件中添加了一个新属性作为可扩展属性,例如属性网格中的字体属性。在 Windows 窗体应用程序项目中使用我的自定义控件后,我在属性网格中看到一个省略号 (…) 按钮,如字体属性的“…”按钮
我在自定义控件中添加了一个新属性作为可扩展属性,例如属性网格中的字体属性。在 Windows 窗体应用程序项目中使用我的自定义控件后,我在属性网格中看到一个省略号 (…) 按钮,如字体属性的“…”按钮
我尝试将 Twitter 上的“Tweet Me”按钮 ( http://twitter.com/goodies/tweetbutton ) 添加到我的网站。然而,每当按下按钮时,我都会收到此 Jav
我试图在我的文本区域中获取一个按钮值,如果我使用 则工作正常但如果我使用那么它就不起作用了。你能找出问题所在吗? HTML 1 2 3 4 JavaScript $(document).read
我的 C# Winform 面板中有一堆文本框。每行文本框的命名如下: tb1 tbNickName1 comboBox1 tb2 tbNickName2 comboBox2 tb3 tbNickNa
我有一个表单,其中过滤器下方有按钮(应用过滤器和清除过滤器),我试图在单击“应用”按钮时显示“清除”,并在单击“清除”按钮时隐藏“清除”按钮。 下面的代码(如果我的表有的话):
excel 按钮正在工作,但是当我添加 pdf 按钮时,它添加仅显示 pdf 按钮 excel 按钮在 pdf 按钮添加后隐藏 $(document).ready(function() { $
我想创建一个 div 作为标题并分成 3 列,并按以下顺序在其中放置 2 个按钮和一个标题:Button1(左位) Title(居中) Button2(右位) 这是我为这个 div 编写的代码:
仅当选中所有框时才应禁用“允许”按钮。我该怎么做?我已经完成了 HTML 部分,如下所示。如何执行其中的逻辑部分?即使未选中一个复选框,也应禁用“允许”按钮
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
如您所知,您可以使用 2 种方法在 HTML5 中呈现按钮: 使用 void 元素 或 如果您需要内容,请使用 元素(不是空元素)。 在JSF2中,有2种方式生成按钮;与UICommand或 UIOu
我尝试根据表单元素的更改禁用/启用保存按钮。但是,当通过弹出按钮选择更改隐藏输入字段值时,保存按钮不受影响。 下面是我的代码。我正在尝试序列化旧的表单值并与更改后的表单值进行比较。但我猜隐藏的字段值无
我有用于在消息(电子邮件、短信)上输入内容的 EditText。我希望在单击 ActionDone 按钮时立即发布消息。我为此使用以下代码: message.setOnEditorActionList
我的 Android 应用程序中有一堆 EditText,每个都将 InputMethod 设置为 numberSigned。我的目标设备没有硬件键盘,而是使用软件键盘输入数字。 Android 将输
我无法以编程方式隐藏弧形菜单中的 fab 按钮。我正在使用https://github.com/saurabharora90/MaterialArcMenu在我的代码中。如何在Java中以编程方式隐藏
我已经看到这在其他类型的对话框窗口中是可能的,例如“showConfirmDialog”,其中可以指定按钮的数量及其名称;但是使用“showInputDialog”时是否可以实现相同的功能?我似乎无法
相同的按钮用于激活和停用。第一次,当代码运行按钮单击并成功“停用”时。但第二次,代码无法找到该元素。第一个案例按钮位于第二个“a”标签中,然后停用第一个“a”标签中的按钮。 案例1: Edit
是否可以将按钮的 onclick 操作设置为 JavaScript 变量?这个想法是我们用 JavaScript 控制一个表。每当点击该表的一行时,我们就会更新一个 JavaScript 变量。该 v
我想创建一个按钮,它包含左侧的文本和右侧的复选框(或任何其他组件)。我怎样才能做到这一点? 我发现我可以制作自己的 View extends Button,但是如果可以的话我应该如何实现 onDraw
我是一名优秀的程序员,十分优秀!