Keep in mind that the button's onAction
is called on the JavaFX thread, therefore you are effectively halting your UI thread for 5 seconds. When the UI thread is un-frozen at the end of these five seconds both changes are applied successively, so you end up only seeing the second.
You can fix this by running all code above in a new thread:
Button button = new Button();
button.setOnAction(event -> {
Thread t = new Thread(() -> {
Platform.runLater(() -> field.setText("START"));
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Platform.runLater(() -> field.setText("END"));
});
t.start();
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…