【发布时间】:2019-12-05 22:08:29
【问题描述】:
这是我在 StackOverflow 上的第一个问题,我感谢任何可以提前提供帮助的人。我正在尝试使用模型视图控制器设计模式来完成一个 javaFX 应用程序。我的应用程序是通过 JavaFX GUI 运行的真/假测验。我将我的 Model 类放在 Main 中(作为子类),但不确定这是否是正确的约定。在任何情况下,我的班级模型如下:
public static class Model{
String question;
Boolean answer;
Model(String question, Boolean answer) {
this.question = question;
this.answer = answer;
}
private static String q1 =
private static String q2 //......String values omitted to make post easier to read.......
private static String q15 =
private static Boolean a1 = //.......Boolean values omitted....
private static Boolean a15 = true;
private static Model[] questions = { //this is the array I want to reference in Controller
new Model(q1, a1),
new Model(q2, a2),
new Model(q3, a3),
new Model(q4, a4),
new Model(q5, a5),
new Model(q6, a6),
new Model(q7, a7),
new Model(q8, a8),
new Model(q9, a9),
new Model(q10, a10),
new Model(q11, a11),
new Model(q12, a12),
new Model(q13, a13),
new Model(q14, a14),
new Model(q15, a15)
};
public static Model[] getQuestions() {
return questions;
}
}
public class Controller {
@FXML
private Label testReadOut;
private Button respondTrue;
private Button respondFalse;
private boolean start = true; //not used in my code, from a tutorial, might be needed later
private Main.Model[] questions = Main.Model.getQuestions();
@FXML
public void takeTest(ActionEvent event) {
int score = 0;
for (int i =0; i<questions.length ; i++) {
!!! testReadOut.setText(questions.question); //question is an unresolved symbol
boolean userAnswer = Boolean.valueOf(((Button)event.getSource()).getText());
!!! if (userAnswer == questions.answer) { //answer is an unresolved symbol
score++;
}
}
testReadOut.setText("User scored: " + score + " out of 15 possible correct answers.");
}
}
The imports used in Controller are:
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
正如我所指出的 !!!在阻止编译的行的开头,当我使用从 Model(Main 的子类)返回所需 Model[] 的 getter 在 Controller 中创建 Model[] 命名问题时,对 Model[] 属性的引用无法识别编译器。
任何帮助将不胜感激!
【问题讨论】:
-
也许this可以帮助你。
-
questions是一个数组。数组不提供您尝试访问的成员。您需要从数组中访问一个元素(可能是questions[i])......顺便说一句,没有评论设计。我现在没有足够的时间......
标签: java arrays javafx model-view-controller subclass