简体   繁体   English

如何在没有按钮的情况下切换 JavaFX 中的场景(倒数计时器)

[英]How to switch scene in JavaFX without button from conclusion of timeline (countdown timer)

Java newbie here building a quiz app. Java 新手在这里构建一个测验应用程序。 I have no trouble switching between scenes from a button, but am trying to switch between scenes at the end of a timer which is controlled by a Timeline.我可以通过按钮在场景之间切换,但我试图在由时间轴控制的计时器结束时在场景之间切换。 So the event is slightly different.所以事件略有不同。 I am having some trouble and keep getting an error.我遇到了一些麻烦,并且不断出现错误。 The timer runs through the 'runTimer' method and the 'startQuiz' method should in theory switch the scenes.计时器通过“runTimer”方法运行,“startQuiz”方法理论上应该切换场景。

Any help would be an absolute lifesaver!任何帮助都将是绝对的救星!

Code posted here excluding imports (but I have included all the ones the IDE suggested).此处发布的代码不包括进口(但我已包括 IDE 建议的所有代码)。

public class countDownScreenController implements Initializable {
private Stage stage;
private Scene scene;
private Parent root;
private StringProperty timerText;
private int timerTotal;
private Timeline timeLine;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    runTimer();
}

public void runTimer(){
    timerTotal = 3;
    setTimerText(timerTotal);
    timeLine = new Timeline();
    timeLine.setCycleCount(timerTotal);
    timeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e -> {
        timerTotal--;
        setTimerText(timerTotal);
    }));
    timeLine.play();
    timeLine.setOnFinished(e -> {
        startQuiz();
    });
}
public void startQuiz (ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/quizScreenScreen.fxml"));
    stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
    stage.setScene(scene);
    stage.show();
}

public countDownScreenController() {
    timerText = new SimpleStringProperty();
}

public String getTimerText() {
    return timerText.get();
}

public StringProperty timerTextProperty() {
    return timerText;
}

public void setTimerText(String timerText) {
    this.timerText.set(timerText);
}

public void setTimerText(int remainingSeconds) {
    int minutes = remainingSeconds / 60;
    int seconds = remainingSeconds % 60;
    setTimerText(String.format("%02d:%02d", minutes, seconds));
}

} }

Try in this order, but use your scene.按此顺序尝试,但使用您的场景。

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/quizScreenScreen.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setScene(scene);
stage.show();

In the end I found it just easier to tie the switch scene event to the Label containing the timer/countdown clock, and then I could switch scenes in the way I know using:最后,我发现将切换场景事件与包含计时器/倒计时时钟的 Label 联系起来更容易,然后我可以使用我知道的方式切换场景:

timeLine.setOnFinished(e -> {
        try {
            startQuiz();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    });
}

public void startQuiz() throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/quizScreen.fxml"));
    Parent root = loader.load();
    countDownClock.getScene().setRoot(root);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM