如何在 JavaFX 2 中打开另一个窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20963700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:35:56  来源:igfitidea点击:

How to open another window in JavaFX 2?

javajavafxjavafx-2

提问by

I have made a simple JavaFX application, but I would like the main window to open a secondary window when the user clicks a button.

我制作了一个简单的 JavaFX 应用程序,但我希望主窗口在用户单击按钮时打开一个辅助窗口。

What would be the simplest way to accomplish this?

实现这一目标的最简单方法是什么?

采纳答案by Alexander Kirov

Button b = new Button();
b.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        Stage stage = new Stage();
        //Fill stage with content
        stage.show();
    }
});

回答by ALI Zidelkhir

try this

尝试这个



try {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXML.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root1));
    stage.show();
    ((Node) (event.getSource())).getScene().getWindow().hide();
} catch (Exception e) {
    e.printStackTrace();
}