package com.along.altunicom.test; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class StageTest extends Application { Test test = new Test(); boolean b = true; int i = 0; int j = 0; public void start(Stage stage) { test.init(); Group root = new Group(); Scene scene = new Scene(root, 100, 100); Button btn = new Button("Show"); btn.setMinSize(100, 100); btn.setOnAction(new EventHandler() { public void handle(ActionEvent arg0) { try { while (true) { // show and count i++; test.show(); Thread.sleep(100); // hide and count j++; test.hide(); Thread.sleep(100); i++; // console System.out.println("Work fine, show count=" + i + ", hide count=" + j); } } catch (Exception e) { // exception, about 15610 times // java.lang.RuntimeException: could not create platform window System.out.println("Exception, show count=" + i + ", hide count=" + j); e.printStackTrace(); } } }); root.getChildren().add(btn); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } public class Test { Stage stage; Scene scene; AnchorPane anchorpane; public void init() { anchorpane = new AnchorPane(); stage = new Stage(); Scene scene = new Scene(anchorpane, 655, 354); stage.setTitle("Stage Test"); stage.setResizable(false); stage.setScene(scene); } public void show() { AnchorPane ap = new AnchorPane(); anchorpane.getChildren().add(ap); stage.show(); } public void hide() { ((AnchorPane) anchorpane.getChildren().get(0)).getChildren() .clear(); anchorpane.getChildren().clear(); stage.hide(); } } }