我正在尝试在我的EntryPoint中安装Java Timer:
Timer timer = new Timer();
timer.schedule(
new TimerTask() {
public void run() {
//some code
}
}
, 5000);
但是在尝试编译时我得到了:
类型java.util.Timer
没有可用的源代码;你忘了吗
继承一个必需的模块?
我该怎么做才能解决此错误?
答案 0 :(得分:12)
在GWT中,您只能使用所有Util包类。
以下是只能在util类中使用的List of Classes。
您可以使用GWT Timer class。
示例(来自docs );
public void onClick(ClickEvent event) {
// Create a new timer that calls Window.alert().
Timer t = new Timer() { //import (com.google.gwt.user.client.Timer)
@Override
public void run() {
Window.alert("Nifty, eh?");
}
};
// Schedule the timer to run once in 5 seconds.
t.schedule(5000);
}
答案 1 :(得分:5)
如果您使用的是Libgdx,则可以使用the libgdx Timer
infrastructure来安排将来运行的工作:
Timer t = new Timer();
t.scheduleTask(new Timer.Task() {
public void run() { /* some code */ }
}), /* Note that libgdx uses float seconds, not integer milliseconds: */ 5);
这样您就可以在与平台无关的代码中安排计时器。 (特定于GWT的解决方案仅适用于项目的平台相关部分。)