- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个小测验游戏,但是当我尝试将值从一个 Controller 传递到另一个 Controller 时,答案总是与我的预期相反。基本上,选择一个单选按钮,如果选择正确的单选按钮,则 returnValue()
方法应该返回 1,否则返回 0。这告诉我用户是否选择了正确的答案。
Controller 1 的代码:
public class Question1
{
@FXML
private Label question1Lbl;
@FXML
private RadioButton rb1;
@FXML
private RadioButton rb2;
@FXML
private RadioButton rb3;
private static int score;
public void nextQuestion(ActionEvent actionEvent)
{
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("../gui/Question2.fxml"));
Parent rootPane = loader.load();
Stage primaryStage = new Stage();
primaryStage.setScene(new Scene(rootPane));
primaryStage.setResizable(false);
primaryStage.setTitle("Frage 2");
primaryStage.show();
rb1.getScene().getWindow().hide();
}
catch (Exception e)
{
System.out.println("Error loading question 1" + e.toString());
e.printStackTrace();
}
}
//returns the points for this question. 1 for correct, 0 for incorrect
public int returnValue()
{
if (!rb2.isSelected())
{
score = 0;
return score;
}
score = 1;
return score;
}
}
FinishScreen
类(class)应该统计从所有 returnValue()
收集的分数。方法并显示分数,但无论我尝试什么,Question1
无论单选按钮是否被选中,class 总是返回 0!我以前写过这样的代码并且运行良好,所以我很困惑。我还有 4 个这样的类(class)。
无论如何,这是FinishScreen
类:
public class FinishScreen implements Initializable
{
@FXML
private Label resultLbl;
private int totalScore;
public void calculateScore() throws Exception
{
FXMLLoader loader1 = new FXMLLoader(getClass().getResource("../gui/Question1.fxml"));
Parent root1 = loader1.load();
Question1 q1 = loader1.getController();
totalScore = q1.returnValue();
System.out.println(totalScore);
resultLbl.setText("Score: " + totalScore);
}
@Override
public void initialize(URL location, ResourceBundle resources)
{
try
{
calculateScore();
}
catch (Exception e)
{
System.out.println("Error in results " + e.toString());
}
}
}
我使用这篇文章中的信息作为如何将值从一个 Controller 传递到另一个 Controller 的引用: FXMLLoader getController returns NULL?
如果我尝试从 Question1
获取值的方式出现错误类是有意义的,但我读过的大多数其他帖子似乎也遵循这种设计模式。这是一个愚蠢的问题,但我无法弄清楚。
我的returnValue()
当 rb2.isSelected() == true
时应该返回 1但事实并非如此。我尝试制作 score
成员变量 static 也是如此,但这没有帮助。这里可能出现什么问题?
最佳答案
即使您使用static
字段来存储结果,当您调用returnValue
方法时,也会从场景中的控件中检索这些结果。
按照 calculateScore
方法中的方式第二次加载 fxml 会创建场景的新版本。在这个新版本的场景中,无论用户在其他版本的场景中完成什么输入,控件都处于初始状态。
我建议稍微重新设计应用程序:
示例
private int questionIndex = -1;
@Override
public void start(Stage primaryStage) throws Exception {
StackPane questionContainer = new StackPane();
questionContainer.setAlignment(Pos.TOP_LEFT);
questionContainer.setPrefSize(300, 300);
// create question list repeating our only question
List<URL> questions = new ArrayList<>();
URL url = getClass().getResource("/my/package/question1.fxml");
for (int i = 0; i < 5; i++) {
questions.add(url);
}
List<QuestionController> questionControllers = new ArrayList<>(questions.size());
Button next = new Button("next");
EventHandler<ActionEvent> handler = evt -> {
if (questionIndex + 1 < questions.size()) {
questionIndex++;
// load next question & store controller for later evaluation
FXMLLoader loader = new FXMLLoader(questions.get(questionIndex));
try {
questionContainer.getChildren().setAll(loader.<Node>load());
questionControllers.add(loader.getController());
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
// display results
Alert alert = new Alert(AlertType.INFORMATION);
alert.setContentText("You've answered "
+ questionControllers.stream().mapToInt(QuestionController::getScore).sum()
+ " / " + questionControllers.size()
+ " questions correctly.");
alert.showAndWait();
primaryStage.close();
}
};
next.setOnAction(handler);
// activate first question
handler.handle(null);
primaryStage.setScene(new Scene(new VBox(questionContainer, next)));
primaryStage.show();
}
public interface QuestionController {
int getScore();
}
public class Question1Controller implements QuestionController {
private static final Random random = new Random();
private int correctAnswer;
@FXML
private List<RadioButton> buttons;
@FXML
private void initialize() {
// randomly assign correct answer
correctAnswer = random.nextInt(buttons.size());
for (RadioButton btn : buttons) {
btn.setText("incorrect");
}
buttons.get(correctAnswer).setText("correct");
}
@Override
public int getScore() {
return buttons.get(correctAnswer).isSelected() ? 1 : 0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.util.ArrayList?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.RadioButton?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.package.Question1Controller">
<children>
<RadioButton fx:id="b1" />
<RadioButton fx:id="b2" />
<RadioButton fx:id="b3" />
<fx:define>
<ArrayList fx:id="buttons">
<fx:reference source="b1"/>
<fx:reference source="b2"/>
<fx:reference source="b3"/>
</ArrayList>
</fx:define>
</children>
</VBox>
关于JavaFX - 在 Controller 类之间传递 int 值会产生不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604682/
我在 Chrome 上做了一些测试,requestAnimationFrame 产生了 61 fps 而 setTimeOut( callback, 0 ) 产生了 233 fps。 如果一个人想要超
当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我在 R Studio 中有一个时间序列。现在我想计算这个系列的log()。我尝试了以下方法: i <- (x-y) ii <- log(i) 但是我得到以下信息:Warning message: I
我有兴趣了解 JavaScript 的内部结构.我试图阅读 SpiderMonkey 的来源和 Rhino但是绕过我的头是相当复杂的。 我问的原因是:为什么像 (![]+[])[+!![]+[]] 生
我们在 Delphi 中使用标准 TWebbrowser 组件,该组件在内部使用 mshtml.dll。另外,我们使用注册表来确保页面使用新的渲染引擎( Web-Browser-Control-Spe
我必须实现一个序列化/反序列化类,并且我正在使用 System.Xml.Serialization 。我有一些IList类型属性并希望在 IList 中序列化解码属于具有特定区域性信息的列表的所有十进
我有一个 Java 应用程序,它读取包含 SQL 查询的 JSON 文件,并使用 JDBC 在数据库上触发它们。 现在我有 5 万个这样的文件,我需要生成 5 万个独立线程来读取每个文件并将它们上传到
我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码: import numpy as
申请后KernelPCA到我的数据并将其传递给分类器 ( SVC ) 我收到以下错误: ValueError: Input contains NaN, infinity or a value too
这背后的想法是,如果我的数据库中存在登录名(正确的用户名+密码),我将重定向到一个页面,并且在进行此身份验证后,他们可以将消息存储在文本文件中。代码非常简单尽管我不确定为什么会收到 IllegalSt
我有一个返回 log10 值的函数。在将它们转换为正常数字时,出现溢出错误。 OverflowError: (34, 'Numerical result out of range') 我检查了日志值,
nosetests 抛出一个 ImportError,尽管我认为这是一个正确配置的 virtualenv。 ==============================================
我是这个网站的新手,所以如果我做错了什么,我提前道歉。当我尝试使用 kivy-garden 的 ScrollLabel 时,它给了我一个错误。基本上我正在尝试创建一个控制台日志,并且我需要能够在文本框
任何人都对 MDSJ 有任何经验?以下输入仅产生 NaN 结果,我不明白为什么。文档非常稀少。 import mdsj.Data; import mdsj.MDSJ; public class MDS
我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ER
我正在使用 Hero 组件在两个页面之间创建动画。Hero 组件用于包装一个 Image 小部件(没问题)和一个 Container 小部件(有问题)。 抛出以下溢出错误: ══╡ EXCEPTIO
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
当在 Angular 中使用不立即触发事件的异步管道时(http 请求或任何有延迟的可观察对象),第一个值为 null为什么会这样?如何避免这种情况? 第一个变化: SimpleChange {
如果一个导入的库生成了一个会 panic 的 goroutine 怎么办?在这种情况下,开发人员无法阻止程序退出。 就像在这段代码中一样,使用延迟恢复调用一个错误的库没有帮助,因为该库正在生成一个 p
我是一名优秀的程序员,十分优秀!