无法正确调整嵌套Pane JavaFX的大小

ilu*_*uke 6 java javafx-2

我目前的情况:一个主要的窗户BorderPane.在它的中心,我有AnchorPane一些内部的物体.我想在窗格的高度内均匀分布对象,即使调整窗格大小.

我搞砸的问题是,当调整大小增加窗格的高度时,所有事情都有效.当我减小窗口的大小时,窗格的高度继续增加.

我在这个示例应用程序中使用一个简单的行重现了错误(在我的应用程序中我也是这样的一行):

import javafx.application.Application;

import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class ResizeProblem extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        AnchorPane inner = new AnchorPane();

        Line line = new Line();

        // comment from here...
        root.setCenter(inner);
        Pane pane = inner;
        // ...to here and uncomment next to make things work
        //Pane pane = root;

        line.startXProperty().bind(pane.widthProperty().divide(2));
        line.endXProperty().bind(pane.widthProperty().divide(2));
        line.setStartY(0.);
        line.endYProperty().bind(pane.heightProperty());

        pane.heightProperty().addListener((obs, ov, nv) -> System.out.println("ov: " + ov + " nv: " + nv));

        pane.getChildren().add(line);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Resize Problem");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经看到避免嵌套,一切都按预期工作.

这有什么问题?

请帮我找到正确的方向.

Ell*_*ltz 2

这是我的解释..

BorderPane不会根据其布局调整其子级的大小Bounds,它很粗心,同样适用AnchorPane,甚至Pane,当您有一个嵌套时,您的行终点与 的Pane高度相匹配,因此它会自动遵循对其限制的任何值,但是如果你把一个人窝AnchorPane在一个人里面,BorderPane谁来照顾孩子?BorderPaneAnchorPane是一个糟糕的婚姻,除非你想把他们放在契约婚姻中,你手动检查每个人的身高和对齐方式,因此 的身高BorderPane增加,并且 的AnchorPane作用相同,并且你的尊重匹配是父母。另外,您的static参考innerPane,并且Pane您必须手动将您的孩子放置在您的 中NodePane不会为您执行此操作,因此使用Pane意味着您的孩子将成为街头儿童。

希望有帮助