【发布时间】:2016-01-14 22:47:38
【问题描述】:
为了效果,我想要一个标签来显示时间,就像秒表一样。时间从 0 开始,到 2 或 3 秒左右结束。需要时停止服务。我遇到的问题是因为我试图每秒更新文本 1000 次,计时器滞后。
就像我说的,这是为了效果,一旦计时器停止就会隐藏,但时间应该相当准确,而不是落后 3 秒。
有什么办法可以加快速度吗?如果可能,我希望保留所有 4 位小数。
timer = new Label();
DoubleProperty time = new SimpleDoubleProperty(0.0);
timer.textProperty().bind(Bindings.createStringBinding(() -> {
return MessageFormat.format(gui.getResourceBundle().getString("ui.gui.derbydisplay.timer"), time.get());
}, time));
Service<Void> countUpService = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
while (!isCancelled()) {
Platform.runLater(() -> time.set(time.get() + 0.0001));
Thread.sleep(1);
}
return null;
}
};
}
};
【问题讨论】: