解决办法:
Platform.runLater(() -> {});
package test;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
public class AlertInThreadExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
new Thread(() -> {
// Your background thread logic here...
System.out.println("hello world");
// Run UI-related code on the JavaFX Application Thread
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("This is an information alert");
alert.setContentText("Hello, world!");
alert.showAndWait();
});
}).start();
}
}