为了在新窗口中显示一些内容,Stage需要创建一个。创建和初始化后show或showAndWait需要在Stage对象上调用:
// 创建样本内容 Rectangle rect = new Rectangle(100, 100, 200, 300); Pane root = new Pane(rect); root.setPrefSize(500, 500); Parent content = root; // 创建包含内容的场景 Scene scene = new Scene(content); Stage window = new Stage(); window.setScene(scene); // 使窗口可见 window.show();
注意:此代码需要在JavaFX应用程序线程上执行。