问题
In Java Swing I used the Swing Timer (not util Timer) to do it. The code looks like this:
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
// "Hello there" is printed out after the 1000 miliseconds
System.out.Println("Hello there");
} catch (Exception e1) {
}
}
});
timer.setRepeats(false);
timer.start();
But I heard it's best not to use Swing imports in JavaFX programs. I've searched so much but I still can't find out how I can do this in JavaFX.
There's the Timer from java.util. But I'm not able to figure out how to do use it to set a time and task that should be done after the time.
Some relevant info:
1) I'm developing a GUI program. This runs in the background. Mostly I'd be using it to play an audio after a pause.
2) I'm very much a newbie to JavaFX.
Thanks a lot.
回答1:
For JavaFX operations, consider using Timeline:
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> System.out.println("Printed after 1 second from FX thread")));
timeline.play();
来源:https://stackoverflow.com/questions/49611162/how-can-i-use-a-timer-to-pause-a-program-for-a-second-before-doing-something-in