我正在创建一个项目,该项目将允许用户参加考试。考试将定时进行,因此我想实现一个计时器,并通过标签向用户显示该时间。我将在initializes类中实现计时器,并且时间到时测试将停止。我在文档方面有些麻烦,想知道我是否在正确的部分和正确的方法中启动代码。
package cais240courseproj;
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.PauseTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Timer;
import java.util.TimerTask;
public class QuestionsController implements Initializable {
QuestionsDataFactory test = new JSONQuestionDataFactory();
List<QuestionsData> data = test.getQuestionsData();
List<String> possibleAnswers;
int count = 0;
ToggleGroup group = new ToggleGroup();
Toggle selectedAnswer[] = new Toggle[data.size()];
@FXML
private Label timeLabel;
@FXML
private Label savedAnswer;
@FXML
private Button logOut;
@FXML
private Button nextBtn;
@FXML
private Button prevBtn;
@FXML
private Label questionText;
@FXML
private RadioButton answerOne;
@FXML
private RadioButton answerTwo;
@FXML
private RadioButton answerThree;
@FXML
private RadioButton answerFour;
@FXML
private void loadLogin(ActionEvent event) throws IOException {
Parent QuestionPane = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene tableViewScene = new Scene(QuestionPane);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
private void loadNextQuestion(ActionEvent event) throws IOException {
if (count < data.size() - 1)
{
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
group.getSelectedToggle().setSelected(false);
}
if (selectedAnswer[count + 1] != null) {
group.selectToggle(selectedAnswer[count + 1]);
}
}
if (count == data.size() - 1) {
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
else {
count++;
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
}
@FXML
private void loadPreviousQuestion(ActionEvent event) throws IOException {
if (count > 0)
{
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
group.getSelectedToggle().setSelected(false);
}
if (selectedAnswer[count - 1] != null) {
group.selectToggle(selectedAnswer[count - 1]);
}
}
if (count == 0) {
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
else {
count--;
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
}
@FXML
private void endTest(ActionEvent event) throws IOException {
ArrayList<User> users = User.deserializeUsers();
int[] answers = new int[data.size()];
double correct = 0;
int userID = FXMLDocumentController.currentUserID;
NumberFormat formatter = new DecimalFormat("#0.00");
User currentUser = users.get(userID);
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
}
for (int i = 0; i < data.size(); i++) {
if (selectedAnswer[i] != null) {
if (selectedAnswer[i].getUserData().equals(0)) {
answers[i] = 0;
}
if (selectedAnswer[i].getUserData().equals(1)) {
answers[i] = 1;
}
if (selectedAnswer[i].getUserData().equals(2)) {
answers[i] = 2;
}
if (selectedAnswer[i].getUserData().equals(3)) {
answers[i] = 3;
}
}
else {
answers[i] = 4;
}
if (answers[i] == data.get(i).getCorrectAnswerIdx()) {
correct++;
}
}
double score = correct / (double)data.size() * 100;
currentUser.setScores(score);
currentUser.setAnswers(answers);
currentUser.setTests(data);
User.serializeUsers(users);
System.out.println("Number of Tests taken: " + currentUser.getNumTests());
for (int i = 0; i < currentUser.getNumTests(); i++) {
System.out.println("Score for test " + (i + 1) + ": " + formatter.format(currentUser.getScores(i)) + "%");
}
Parent QuestionPane = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene tableViewScene = new Scene(QuestionPane);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
private void saveAnswer(ActionEvent event) throws IOException {
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
savedAnswer.setVisible(true);
PauseTransition pause = new PauseTransition();
pause.setDuration(Duration.seconds(1.4));
pause.setOnFinished(e ->{
savedAnswer.setVisible(false);
});
pause.play();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
answerOne.setUserData(0);
answerTwo.setUserData(1);
answerThree.setUserData(2);
answerFour.setUserData(3);
answerOne.setToggleGroup(group);
answerTwo.setToggleGroup(group);
answerThree.setToggleGroup(group);
answerFour.setToggleGroup(group);
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
Timer time = new Timer();
TimerTask tt = new TimerTask()
{
int secondsPassed = 0;
@Override
public void run()
{
secondsPassed++;
}
};
time.schedule(tt, count);
}
}
在右下角说标签是要实现计时器的地方