单击 myButton 后,运行 2 个线程。首先更改按钮图像,第二个 - 循环。我希望我的程序首先更改图像并在运行循环之后。但这不行,他们一瞬间就完蛋了。
myButton.setOnMouseClicked(event -> {
if (event.getButton() == MouseButton.PRIMARY) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
ImageView imageView = new ImageView(image);
imageView.setFitHeight(my_button_height - 16);
imageView.setFitWidth(my_button_width - 16);
myButton.setStyle("-fx-background-color: #ffffff00; ");
myButton.setGraphic(imageView);
}
});
thread.run();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 15; i++){
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
thread1.run();
}
});
}
我尝试添加.join。这没有帮助