立即将 Node.height 更改为 Node.prefHeight (AnchorPane)
Change Node.height instantly to Node.prefHeight (AnchorPane)
在我的 fxml 中,我在 AnchorPane 中有一个 FlowPane:
AnchorPane 应在 FlowPane 需要另一行时立即更改其高度。我试图在 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
我不敢相信我是第一个问这个问题的人。
将底部和顶部锚点设置为 0
将导致 AnchorPane
的高度自动调整大小,除非您有一些其他布局限制来阻止这种情况:
AnchorPane.setTopAnchor(flowPane, 0.0);
AnchorPane.setBottomAnchor(flowPane, 0.0);
请注意 Pane
s 的实际大小调整是在布局过程中完成的,可以手动触发查看 jewelsea 对这个问题的回答:Get the height of a node in JavaFX (generate a layout pass) .
在我的 fxml 中,我在 AnchorPane 中有一个 FlowPane:
AnchorPane 应在 FlowPane 需要另一行时立即更改其高度。我试图在 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
我不敢相信我是第一个问这个问题的人。
将底部和顶部锚点设置为 0
将导致 AnchorPane
的高度自动调整大小,除非您有一些其他布局限制来阻止这种情况:
AnchorPane.setTopAnchor(flowPane, 0.0);
AnchorPane.setBottomAnchor(flowPane, 0.0);
请注意 Pane
s 的实际大小调整是在布局过程中完成的,可以手动触发查看 jewelsea 对这个问题的回答:Get the height of a node in JavaFX (generate a layout pass) .