【问题标题】:Javafx open another fxml in the another window with buttonJavafx 用按钮在另一个窗口中打开另一个 fxml
【发布时间】:2015-01-25 11:27:38
【问题描述】:

是否可以在 javafx 中通过按钮从另一个 fxml 打开新阶段(窗口)? 感谢您的回答。

【问题讨论】:

  • 是的。您能否展示一些代码并解释您遇到的问题?

标签: javafx window fxml stage


【解决方案1】:

在按钮点击时使用下面的代码:

try {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Demo.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.initModality(Modality.APPLICATION_MODAL);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setTitle("ABC");
    stage.setScene(new Scene(root1));  
    stage.show();
}

【讨论】:

  • 谢谢你这个代码,但我想要一个窗口可以单独移动并关闭右上角。
  • 移动和关闭带有样式的窗口注释行:StageStyle.UNDECORATED
  • 将stage.onCloseRequestProperty().setValue(e -> Platform.exit()); 添加到启动窗口的阶段也是一个好主意,以便在按下关闭按钮时退出应用程序并关闭所有其他窗口。
【解决方案2】:

我不得不稍微修改一下代码,它工作正常。 再次感谢您提供代码!

public void pressButton(ActionEvent event) throws Exception {               
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/A.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));  
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多