Binding 绑定动态加载的fxml的宽度和高度

Binding 绑定动态加载的fxml的宽度和高度,binding,javafx-2,fxml,Binding,Javafx 2,Fxml,目前,我在运行时动态加载FXML文件时遇到了一个问题。将它们添加到窗格后,它们不会调整大小以使用该窗格的完整宽度和高度 我使用此方法在我的窗格中加载FXML: public void showContentPane(String sURL){ //1. sURL can be something like "/GUI/home/master/MasterHome.fxml" //2. getContentPane() returns following object: Pane

目前,我在运行时动态加载FXML文件时遇到了一个问题。将它们添加到窗格后,它们不会调整大小以使用该窗格的完整宽度和高度

我使用此方法在我的窗格中加载FXML:

public void showContentPane(String sURL){
    //1. sURL can be something like "/GUI/home/master/MasterHome.fxml"
    //2. getContentPane() returns following object: Pane pContent
    try {
        URL url = getClass().getResource(sURL);

        getContentPane().getChildren().clear();
        Node n = (Node) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));

        getContentPane().getChildren().add(n);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}
FXML被加载并正常工作,但是我注意到FXML(在本例中作为节点添加)的大小没有调整到使用内容窗格的完整高度和宽度(如果我使用场景生成器在预览模式下打开FXML,它的大小会完全调整)。这是一种错误的方法,还是有一种我显然没有找到的简单方法


提前谢谢

我根据安迪给我的线索调整了密码。我将pContent对象更改为AnchorPane,而不是窗格。方法的工作原理如下:

public void showContentPane(String sURL){
    //1. sURL can be something like "/GUI/home/master/MasterHome.fxml"
    //2. getContentPane() returns following object: AnchorPane pContent
    try {
        URL url = getClass().getResource(sURL);

        getContentPane().getChildren().clear();

        //create new AnchorPane based on FXML file
        AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));

        //anchor the pane
        AnchorPane.setTopAnchor(n, 0.0);
        AnchorPane.setBottomAnchor(n, 0.0);
        AnchorPane.setLeftAnchor(n, 0.0);
        AnchorPane.setRightAnchor(n, 0.0);

        getContentPane().getChildren().add(n);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

提示:确保在加载的FXML文件中不使用固定的宽度或高度变量。

我根据安迪给我的线索调整了代码。我将pContent对象更改为AnchorPane,而不是窗格。方法的工作原理如下:

public void showContentPane(String sURL){
    //1. sURL can be something like "/GUI/home/master/MasterHome.fxml"
    //2. getContentPane() returns following object: AnchorPane pContent
    try {
        URL url = getClass().getResource(sURL);

        getContentPane().getChildren().clear();

        //create new AnchorPane based on FXML file
        AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));

        //anchor the pane
        AnchorPane.setTopAnchor(n, 0.0);
        AnchorPane.setBottomAnchor(n, 0.0);
        AnchorPane.setLeftAnchor(n, 0.0);
        AnchorPane.setRightAnchor(n, 0.0);

        getContentPane().getChildren().add(n);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

提示:请确保在加载的FXML文件中不要使用固定的宽度或高度变量。

这取决于容器,但如果要将其添加到锚具上,则需要将角固定到父级。我花了一段时间才弄明白,在它起作用之前,必须进行一些UI更改。我将在下面添加一个答案,具体取决于容器,但如果它被添加到锚具上,那么您需要将角落锚定到父级。我花了一段时间才弄明白,在它起作用之前,必须进行一些UI更改。我将在下面添加一个答案