我正在编写一个用于管理声音的应用程序,其中使用了很多指标,其中一个指标是“剪辑”指标。
它的工作方式是:如果声音水平超过极限,则称为“ setClipping”的方法将更改圆形的颜色(在下面的代码中称为“ clip”),以将其从深红色变为红色。
当该指示灯变为红色时,它将保持红色500 ms,然后返回深红色。
指示器为红色时,如果再次调用方法“ setClipping”,则任务被取消;然后,建立并启动一个新的。
这是方法的代码:
public void setClipping(boolean clipping) {
// The Task "task" and Circle "clip" are declared earlier in my class
if (clipping) {
Platform.runLater(() -> {
if (task!=null && task.isRunning()) {
task.cancel();
}
});
task = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("Going red !");
clip.setFill(Color.RED);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Back to dark...");
clip.setFill(Color.RED.interpolate(Color.RED, 0.5));
return null;
}
};
new Thread(task).start();
}
}
错误是:有时,任务结束时“剪贴画圆”没有改变颜色,但是却调用了“ System.out.println(...)”方法。
我的代码有什么问题,您会给我什么提示来解决我的问题?