简体   繁体   English

如何使用TimerTask将旧的Java Timer更新为新的Java Timer

[英]How to update old Java Timer to new Java Timer with a TimerTask

I have some older Java code that looks like this: 我有一些较旧的Java代码,如下所示:

timer = new Timer( 1000, (ActionListener) this );

In the newest Java (I'm using Java 8 build 129 and JavaFX), Timer needs a TimerTask and I'm not sure how to attach that to my ActionListener... 在最新的Java中(我使用的是Java 8 build 129和JavaFX),Timer需要一个TimerTask,但我不确定如何将其附加到我的ActionListener上。

There are two Timer classes in the jdk: java.util.Timer and javax.swing.Timer . jdk java.util.Timer有两个Timer类: java.util.Timerjavax.swing.Timer The one that takes an ActionListener is the latter. 使用ActionListener的是后者。 The one that works with TimerTasks is the former. 与TimerTasks一起使用的是前者。

Note that the constructor's signature has not changed in Java 8 . 注意,构造函数的签名在Java 8中没有更改。

So you should probably check your imports. 因此,您可能应该检查进口。

NOTE: since you are upgrading to Java 8, you may as well take the opportunity to avoid obsolete classes (cf. javadocs) and use a ScheduledExecutorService . 注意:由于要升级到Java 8,因此最好还是避免使用过时的类(请参阅javadocs),并使用ScheduledExecutorService

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

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