【问题标题】:FXML to open a new scene via buttonFXML 通过按钮打开新场景
【发布时间】:2017-10-19 13:15:04
【问题描述】:

我正在使用 Scene Builder 创建 JavaFX GUI 应用程序。我正在尝试使用 FXML 实现类似的东西:

reportButton = new Button("Report");
reportButton.setOnAction(e -> ReportPage.display());

但我不知道如何使用控制器页面来做到这一点。有人可以告诉我该怎么做吗? 谢谢

【问题讨论】:

  • 请发布更多代码和 fxml 文件...这实际上取决于您如何实现不同的视图。他们有共同的父母观点吗?
  • 只需在控制器中定义合适的方法即可。请参阅documentation
  • 你是在尝试开启一个新的舞台吗?改变当前场景?将视图放在当前阶段的容器中?
  • 问之前先用谷歌

标签: java javafx fxml scenebuilder scene


【解决方案1】:

这里是如何显示一个新的阶段。将此代码添加到您的 on action 函数中
(您可以在代码中使用场景构建器添加该功能:在动作属性上)

@FXML
private void reportButtonHandler(ActionEvent event) {
    FXMLLoader fxmlLoader = new 
        FXMLLoader(getClass().getResource("pathtofxml/ReportPage.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    //set what you want on your stage
    stage.initModality(Modality.APPLICATION_MODAL);
    stage.setTitle("Report Page");
    stage.setScene(new Scene(root1));
    stage.setResizable(false);
    stage.show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多