1

現時点では、実行時に動的にロードされる 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 を開いた場合)ビルダー、それは完全にサイズ変更されます)。これは間違ったアプローチですか、それとも明らかに見つけられなかった簡単な方法がありますか?

前もって感謝します!

4

1 に答える 1

1

アンディがくれた手がかりでコードを調整しました。pContentオブジェクトをPaneではなく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ファイルで、固定の幅または高さの変数を使用していないことを確認してください。

于 2013-01-23T10:33:12.337 に答える