有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java同一阶段,不同Fxml Javafx

我是JavaFX新手。我有一个装有垂直分割窗格的窗口。在拆分页面的左侧,我有两个按钮。在每个按钮上单击我需要在拆分窗格的右侧加载单独的fxml。所以在这里,我粘贴屏幕截图以保持清晰

enter image description here

在这里,现在我在单独的阶段,单独的场景打开时,搜索按钮被点击。现在我需要在baselayout窗口的右侧加载Searcher。下面是一些加载baseLayout的代码

@Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Base Layout");
        BaseController.setMlTool(this);
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Base.fxml"));
        baseLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(baseLayout);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

下面是一些在点击按钮时加载Searcher的代码

@FXML
    private void initialize(){
        System.out.println("Testing");
    }

    @FXML
    private void handleSearchButton(){
        System.out.println("Handle Button Called");

        Stage search = new Stage();
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Searcher.fxml"));
        search.setTitle("Searcher");
        try {
            AnchorPane searcherPage = (AnchorPane) loader.load();
            Scene scene = new Scene(searcherPage);
            search.setScene(scene);
            search.show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这里我如何在BaseLayout的拆分窗格右侧加载Searcher。 希望我的问题清楚。谢谢你的帮助


共 (3) 个答案

  1. # 1 楼答案

    在处理程序方法中,尝试以下操作:

    首先申报锚泊烷。在fxml右侧窗格中,放置带有AnchorCmdController名称的anchorpane

     @FXML
     private AnchorPane anchCmdController;
    

    然后更新handleSearchButton事件

    @FXML
    private void handleSearchButton() throws IOException {
    System.out.println("Handle Button Called");
    
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view/Searcher.fxml"));
    Pane cmdPane = (Pane) fxmlLoader.load();
    try {
    /* NEED TO DECLARE ANCHOR PANE" */
    anchCmdController.getChildren().clear();
    anchCmdController.getChildren().add(cmdPane);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
  2. # 2 楼答案

    您可以选择右侧的窗格,然后将fxml作为该窗格的子节点加载。阶段始终是相同的,您可以在窗格中加载fxml。 例:主窗口。fxml是perent窗口。你可以在后台加载

    Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    

    现在在主窗口。fxml您有拆分窗格。将anchorPan放在您希望在点击按钮时加载fxml的右侧。然后将fxml作为子代添加到anchorpane中

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
    Pane cmdPane = (Pane) fxmlLoader.load();
    try {
    anchCmdController.getChildren().clear();
    anchCmdController.getChildren().add(cmdPane);
    fadeIn.playFromStart();
    } catch (Exception e) {
    e.printStackTrace();
    }
    
  3. # 3 楼答案

    试着听一下按钮的点击,点击后使用“FXMLLoader”。“加载”要根据根元素加载,请键入“显示面板”,并将其分配到右侧的内容