private void run(ActionEvent event) throws InterruptedException {
button.setDisabled(true);
Thread.sleep(3000);
button.setDisabled(false);
}
这是我的代码。
当 Button
被禁用时,我想在 Thread.sleep()
的地方做一些事情。
但它同时完成所有事情。
有人可以帮忙吗?
最佳答案
这是一个使用 Task
的示例在不同的线程上执行操作。
Button b = new Button("Button!");
b.setOnAction(e -> {
b.setDisable(true);
Task<Void> task = new Task<Void>(){
@Override
protected Void call() throws Exception {
Platform.runLater(() -> b.setText("Modified text")); // Use Platform.runLater here to modify the scene graph
Thread.sleep(3000);
return null;
}
};
task.setOnSucceeded(v -> {
System.out.println("Succeed");
b.setText("Button!");
b.setDisable(false);
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
});
关于java - 让事件处理程序立即采取行动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40994137/