父节点中 needsLayout 属性 的用途
Purpose of needsLayout property in Parent node
有人可以让我知道父节点中 "needsLayout" 属性 的目的是什么以及我如何从中受益。我的印象是使用 isNeedsLayout() 会告诉我节点是否在场景图中呈现。但看起来并非如此。而且我也对父API
中的描述感到困惑
needsLayout : Indicates that this Node and its subnodes requires a
layout pass on the next pulse.
任何关于此 属性 的 help/explanation 都非常感谢。谢谢。
布局通道确定场景中节点的位置和大小。如果对场景的更新以需要重新计算其祖先大小的方式对其进行修改,则这些布局通道由 JavaFX 自动安排。不会立即执行布局传递,以避免为连续修改一遍又一遍地重新计算布局。
@Override
public void start(Stage primaryStage) throws IOException {
Button btn = new Button("click");
btn.setPrefWidth(60);
StackPane root = new StackPane(btn);
btn.setOnAction(evt -> {
System.out.println("before modification: " + root.isNeedsLayout());
btn.setPrefWidth(btn.getPrefWidth() + 1);
System.out.println("after modification: " + root.isNeedsLayout());
});
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
在上面的示例中,当您单击按钮时,场景已经更新。布局过程定位按钮并确定它的大小。按钮事件处理程序更新按钮的 prefWidth
属性,这可能会导致 size/layout 发生变化,因此需要进行布局传递。当布局通道发生时,标志被清除,只有在按钮的另一次修改之后,新的布局通道才有必要。
通常你不需要为这个属性操心。您扩展的 Parent
的子类将负责更新 属性.
有人可以让我知道父节点中 "needsLayout" 属性 的目的是什么以及我如何从中受益。我的印象是使用 isNeedsLayout() 会告诉我节点是否在场景图中呈现。但看起来并非如此。而且我也对父API
中的描述感到困惑needsLayout : Indicates that this Node and its subnodes requires a layout pass on the next pulse.
任何关于此 属性 的 help/explanation 都非常感谢。谢谢。
布局通道确定场景中节点的位置和大小。如果对场景的更新以需要重新计算其祖先大小的方式对其进行修改,则这些布局通道由 JavaFX 自动安排。不会立即执行布局传递,以避免为连续修改一遍又一遍地重新计算布局。
@Override
public void start(Stage primaryStage) throws IOException {
Button btn = new Button("click");
btn.setPrefWidth(60);
StackPane root = new StackPane(btn);
btn.setOnAction(evt -> {
System.out.println("before modification: " + root.isNeedsLayout());
btn.setPrefWidth(btn.getPrefWidth() + 1);
System.out.println("after modification: " + root.isNeedsLayout());
});
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
在上面的示例中,当您单击按钮时,场景已经更新。布局过程定位按钮并确定它的大小。按钮事件处理程序更新按钮的 prefWidth
属性,这可能会导致 size/layout 发生变化,因此需要进行布局传递。当布局通道发生时,标志被清除,只有在按钮的另一次修改之后,新的布局通道才有必要。
通常你不需要为这个属性操心。您扩展的 Parent
的子类将负责更新 属性.