在javafx中延迟n秒

时间:2017-10-23 05:57:11

标签: multithreading javafx timer delay

我试图在继续做下一件事之前添加延迟。这是我的代码:

private void startup() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            Platform.runLater(() -> {
                mainPane.setBackground(new Background(new BackgroundFill(Color.rgb(28, 28, 28), null, null)));
                logPane.setBackground(new Background(new BackgroundFill(Color.rgb(28, 28, 28), null, null)));
                startupAnimation();
            });
        }
    }, 3500);
    logMessage(Mavis.LOG_INITIALIZE);

private void startupAnimation() {
    FadeTransition ftMain = new FadeTransition(Duration.millis(1200), mainPane);
    ftMain.setFromValue(0);
    ftMain.setToValue(1);
    ftMain.setCycleCount(1);
    FadeTransition ftLog = new FadeTransition(Duration.millis(1200), logPane);
    ftLog.setFromValue(0);
    ftLog.setToValue(1);
    ftLog.setCycleCount(1);
    ParallelTransition pt = new ParallelTransition();
    pt.getChildren().addAll(ftMain, ftLog);
    TranslateTransition tt = new TranslateTransition(Duration.millis(800), mavisImgView);
    tt.setByX(-210);
    tt.setCycleCount(1);
    FadeTransition ftOver = new FadeTransition(Duration.millis(1500), overlayPane);
    ftOver.setFromValue(0);
    ftOver.setToValue(1);
    ftOver.setCycleCount(1);
    SequentialTransition seq = new SequentialTransition(pt,tt, ftOver);
    seq.play();
}

private void logMessage(String msg) {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            Platform.runLater(() -> {
                logTxtArea.setText(msg);
                chatBalloon.setVisible(true);
            });
        }
    }, 1500);
}   

我想要实现的是,在记录消息之前,GUI必须启动3.5秒,然后在加载GUI时,它将记录消息。问题是该消息已经与GUI的启动一起记录,这似乎是什么问题?使用的计时器是java.util.Timer。

1 个答案:

答案 0 :(得分:1)

对于初始延迟,请使用PauseTransition,然后在SequentialTransition的开头添加。