【问题标题】:Change Node.height instantly to Node.prefHeight (AnchorPane)立即将 Node.height 更改为 Node.prefHeight (AnchorPane)
【发布时间】:2016-10-30 20:31:50
【问题描述】:

在我的 fxml 中,我在 AnchorPane 中有一个 FlowPane:

一旦 FlowPane 需要另一行,AnchorPane 就应该改变它的高度。我试图在 Flowpane.heightProperty 上更改监听器并且它有效。然后我将 AnchorPane 的 prefHeight 设置为 flowPane 的新高度,因为我无法设置高度。问题是 AnchorPane 不会立即调整大小。 prefHeight 会随着我想要的改变而改变,但高度不会改变。在我将鼠标拖到 FlowPane 上后,它会发生变化。

代码:

FlowPane flowPane = new FlowPane();
AnchorPane anchorPane = new AnchorPane(flowPane);

titleAnchorPane.setTopAnchor(titleFlowPane, 0.0);
titleAnchorPane.setLeftAnchor(titleFlowPane, 0.0);
titleAnchorPane.setRightAnchor(titleFlowPane, 0.0);

flowPane.heightProperty().addListener((observable, oldValue, newValue) -> {
            anchorPane.setPrefHeight(newValue.doubleValue()); 
 });

Bevore I dragged my mouse over it

After I dragged my mouse over it

我不敢相信我是第一个问这个问题的人。

【问题讨论】:

    标签: java validation javafx properties refresh


    【解决方案1】:

    将底部和顶部锚点设置为0 将导致AnchorPane 的高度自动调整,除非您设置了一些其他布局约束来防止这种情况发生:

    AnchorPane.setTopAnchor(flowPane, 0.0);
    AnchorPane.setBottomAnchor(flowPane, 0.0);
    

    请注意,Panes 的实际大小调整是在布局过程中完成的,可以手动触发,请参阅 Jewelsea 对此问题的回答:Get the height of a node in JavaFX (generate a layout pass)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多