JavaFX:FXML:如何让孩子扩展其大小以适应父窗格?

我设法在父fxml(mainMenu UI)下加载子fxml(子UI)。 我创建了一个id为“mainContent”的AnchorPane。 此窗格绑定到4个方面,并且更改符合舞台。

子窗口将加载到“mainContent”锚定窗格中。 但是,我无法弄清楚如何让孩子与其父“mainContent”一起改变。

我的孩子UI就是这样调用的。

@FXML private void mnuUserLevel_onClick(ActionEvent event) { FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml")); loader.setController(new DBeditEntityUserlevel()); try { Node n = (Node)loader.load(); mainContent.getChildren().add(n); } catch (IOException e){ System.out.println(e.getMessage()); } } 

为了进一步说明我的问题,请看我的快照。 红场就是孩子。 黄色方块是MainMenu父级的“mainContent”AnchorPane。

在此处输入图像描述

如果将AnchorPane内窗格的topAnchor,bottomAnchor,leftAnchor,rightAnchor值设置为0.0,则面板将被拉伸到周围AnchorPane的完整范围。

文档链接: AnchorPane

编辑:在文档链接中,您还可以看到如何在Java代码中设置这些值。

FXML示例:

    

如果你有一个Region ,它是Node子类 ,包括AxisChartControlPane (这可能包括你可能想要加载的任何内容),你可以子的大小绑定到父级中的空间,类似于它们的方式在这里做 现在,任何未来父母大小的调整都将反映在孩子身上。

 Region n = (Region)loader.load(); mainContent.getChildren().add(n); n.prefWidthProperty().bind(mainContent.widthProperty()); n.prefHeightProperty().bind(mainContent.heightProperty()); 

通过查看界面的结构,我猜你最好使用BorderPane作为你的父容器,这个建议不要直接在BorderPane容器中使用AnchorPane这里是我的建议:

– > BorderPane-Top =“你的菜单”

– > BorderPane-Center =另一个容器(可能是SplitPane,另一个BorderPane等..但我猜不是AnchorPane但你可以尝试,如果你想)

– > BorderPane-Bottom =另一个容器(可以是带有按钮的hbox或信息或通知等的标签……)

您可以尝试设置prefWidth。 我不知道为什么,但似乎需要在将newPane引用到您的AnchorPane之后在SceneBuilder中设置的prefWidth。

 Node newPane = FXMLLoader.load(getClass().getResource("view/YourPane.fxml")); List parentChildren = ((Pane)yourAnchorPaneId.getParent()).getChildren(); parentChildren.set(parentChildren.indexOf(yourAnchorPaneId), newPane); yourAnchorPaneId.setPrefWidth(1800); // 1800 just example value for test Main.primaryStage.setWidth(500); // 500 example value with which you wishe to start app