First, work out the delay of 1 s, and then only the color of the button changes. Where is the mistake?

public void btnClick(ActionEvent actionEvent) { btn.setStyle("-fx-background-image: url('img/Yellow.png');"); Platform.runLater(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); } 

    2 answers 2

      Task<Void> task = new Task<Void>() { @Override protected Void call() throws Exception { //Действия //Если выполняете действия в JavaFX //Пример: Platform.runLater(() -> { // vBox.getChildren().add(finalPane); //}) return null; } }; Thread thread = new Thread(task); //передаём в поток thread.start(); 

      Platform.runLater executes the code in the UI stream, therefore you may have a situation when the picture has loaded, but the UI stream has already fallen asleep and the style has not been updated.