如何在Java-fx中创建一个新的舞台以及如何将该舞台作为弹出窗口?

时间:2016-04-17 15:21:44

标签: java javafx

我想在JavaFX中创建一个新阶段,当我点击一个按钮时,新阶段会弹出一个新的scene

当新的scene弹出前一阶段时会出现,但不起作用。当我关闭弹出窗口时,前一个窗口将起作用。请帮帮我。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码段:

public void start(Stage primaryStage) {
    try {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 400);
        Button btn = new Button(); // create a btn
        root.getChildren().add(btn); // add this btn to the root

        btn.setOnAction(new EventHandler<ActionEvent>() // when click
        {
            @Override
            public void handle(ActionEvent e) {
                Stage dialog = new Stage(); // new stage
                dialog.initModality(Modality.APPLICATION_MODAL);
                // Defines a modal window that blocks events from being
                // delivered to any other application window.
                dialog.initOwner(primaryStage);
                VBox vb = new VBox(20);
                Scene dialogScene = new Scene(vb, 300, 200);
                dialog.setScene(dialogScene);
                dialog.show();
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
相关问题