在MVC程序(JavaFX应用程序)中引用数组项

问题描述 投票:-1回答:1

这是我关于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;

正如我用!!!指示的那样!在防止编译的行的开头,当我使用getter从Model(Main的子类)返回所需的Model []时在Controller中创建名为问题的Model []时,无法识别对Model []属性的引用编译器。

任何帮助将不胜感激!

java arrays javafx model-view-controller subclass
1个回答
0
投票

使用;

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)
    };

在控制器类中。我的意思是在控制器类中创建问题数组。

© www.soinside.com 2019 - 2024. All rights reserved.