在JavaFX中执行某些操作之前,如何使用计时器暂停程序一秒钟?

时间:2018-04-02 12:18:13

标签: javafx timer

在Java Swing中,我使用了Swing Timer(不是util Timer)来实现它。代码如下所示:

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();

但我听说最好不要在JavaFX程序中使用Swing导入。我搜索了这么多,但我仍然无法找到如何在JavaFX中做到这一点。

java.util中有Timer。但我无法弄清楚如何使用它来设置应该在时间之后完成的时间和任务。

一些相关信息:

1)我正在开发一个GUI程序。这在后台运行。大部分时间我都会用它来暂停播放音频。

2)我是JavaFX的新手。

非常感谢。

1 个答案:

答案 0 :(得分:0)

对于JavaFX操作,请考虑使用时间轴:

Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> System.out.println("Printed after 1 second from FX thread")));
timeline.play();