簡體   English   中英

如何在Java-fx中創建一個新舞台,以及如何使該舞台成為彈出窗口?

[英]How to make a new Stage in Java-fx and how to make that stage as pop-up?

我想在JavaFX中建立一個新階段,當我單擊一個按鈕時,新階段將彈出一個新scene

當新scene彈出時,將出現上一個階段,但將不起作用。 當我關閉彈出窗口時,上一個窗口將起作用。 請幫我做到這一點。

您可以嘗試使用以下代碼片段:

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM