你能帮我弄清楚如何在我的测验计划中组织数据吗?

时间:2015-12-18 12:51:34

标签: java javafx

我正在使用JavaFX进行多项选择测验。每个问题都会有问题,可能的答案和正确的答案。下面我有一些工作代码,但它都在一个类中。我意识到我应该在另一个类(或类)中拥有数据,并且我不应该使用并行数组。另一个类还应该包含在问题标签和答案按钮上设置文本的方法,并在用户按下“下一个”时更改它们。我还需要一种方法让用户知道他们的答案是否正确。 问题是我不知道如何组织数据以及如何使其他类中的方法与GUI交互。你能否就如何处理这项任务给我一些想法?

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class Quiz extends Application {

private int questionNumber = 0 ;

private int max = 5;

String[] question = new String[max];

String [] answer1 = new String[max];

String [] answer2 = new String [max];

String [] answer3 = new String[max];

Label label = new Label();

Label userAnswer = new Label();

RadioButton choice1 = new RadioButton();

RadioButton choice2 = new RadioButton();

RadioButton choice3 = new RadioButton();

ToggleGroup answers = new ToggleGroup();

Button next = new Button("Next");

RadioButton userAnswered;

TilePane buttons = new TilePane();

Button answer = new Button("Answer");

public static void main(String[] args) {

    launch(args);
}

@Override

public void start(Stage primaryStage) throws Exception {

    question[0]= "What's the capital  of Monaco?";
    question[1] = "What's the capital of San Marino?";
    question[2] = "What's the capital of Lithuania?";
    question[3] = "What's the capital of Denmark?";
    question[4] = "What's the capital of Finland?";


    label.setFont(new Font("Cambria", 26));
    label.setTranslateY(230);
    label.setTranslateX(230);
    label.setText(question[0]);

    HBox answerArea = new HBox();
    answerArea.setTranslateY(330);
    answerArea.setTranslateX(260);
    answerArea.setSpacing(20);


    answer1 [0] =  "Monte Carlo";
    answer1 [1] =  "San Marino";  
    answer1 [2] =  "Kaunas";
    answer1 [3] =  "Copenhagen";  
    answer1 [4] = "Tampere";


    answer2[0] = "Monaco";  
    answer2[1] = "Kairo";
    answer2[2] = "Warsav";
    answer2[3] = "Oslo";
    answer2 [4] = "Helsinki";  

    answer3 [0]= "Tirana";
    answer3 [1]= "Zagreb";
    answer3 [2]= "Vilnius";   
    answer3 [3]= "Stockholm";
    answer3 [4]= "Turku";



    choice1.setText(answer1[0]);
    choice2.setText(answer2[0]);
    choice3.setText(answer3[0]);

    choice1.setToggleGroup(answers);
    choice2.setToggleGroup(answers);
    choice3.setToggleGroup(answers);

    answer.setMaxWidth(Double.MAX_VALUE);
    answer.setOnAction(e -> questionAnswered());

    answers.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
        @Override
        public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

           userAnswered = (RadioButton) newValue.getToggleGroup().getSelectedToggle(); // Cast objekt  to radiobutton

        }
    });
   userAnswer.setTranslateY(280);
   userAnswer.setTranslateX(360);
   userAnswer.setFont(new Font("Cambria", 16));

    answerArea.getChildren().addAll(choice1, choice2, choice3);



    next.setOnAction(e -> nextQuestion());
    next.setMaxWidth(Double.MAX_VALUE);


    buttons.setTranslateY(400);
    buttons.setTranslateX(280);
    buttons.setHgap(5);

    buttons.getChildren().addA(next);

    answer.setTranslateX(550);
    answer.setTranslateY(330);


    Stage stage = new Stage();
    Pane pane = new Pane();
    pane.setStyle("-fx-background-color: #FFFFFF;");
    Scene scene = new Scene(pane, 800, 500);
    stage.setScene(scene);
    stage.setResizable(false);
    stage.show();

    pane.getChildren().addAll(label, answerArea, buttons, answer, userAnswer);

}


public void questionAnswered(){

    if (userAnswered.equals(choice2)) {
        userAnswer.setText("Correct");
    } else {
        userAnswer.setText("Wrong answer");
    }

}

public void nextQuestion() {
    questionNumber = (questionNumber + 1) % question.length ;
    label.setText(question[questionNumber]);
    choice1.setText(answer1[questionNumber]);
    choice2.setText(answer2[questionNumber]);
    choice3.setText(answer3[questionNumber]);


    userAnswer.setText(" ");

    });

}
  }

1 个答案:

答案 0 :(得分:0)

此类任务提供了OOP和MVC原则的典型示例。正如@UlukBiy指出的那样 - 你甚至在解释中提到了潜在的对象类(eq Question / Answer)。这些课程将构成你的模特。

到目前为止,您所拥有的大部分内容都围绕用户界面(视图)。

第三个组件是控制器(名称类似于QuizController)。这将管理问题库(类似List<Question> questions),并且可以找到nextQuestion()等方法。

值得记住的是,随着问题/答案数量的增加,你会看到使用一些外部数据源(数据库/ xml文件) - 但似乎很难编码这些问题对你来说已经足够了(一些控制器中的populateQuestionBank()方法也许?)

有关MVC模式的更多信息:

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller