[英]JavaFX: getWidth() and getLayoutBounds() return 0
大家好,我正在开发一个简单的编辑器,以使用JavaFX创建一些用例。 现在,当我想将两个节点相互连接时,我想给该链接起一个名字。 当前的想法是将标签放入anchorPane,并将此窗格置于链接中间。 问题在于,无论是getWidth()-还是getLayoutBounds()函数都不会返回正确的结果,而在所有情况下均为0。 我如何找出该anchorPane的宽度? 感谢所有答案!
// Java代码
public class Link extends AnchorPane{
public Link() {
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("../view/Link.fxml")
);
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public void bindAnchorPane(DraggableNode source, DraggableNode target){
//Middlepoint X source and target node
NumberBinding middleX = Bindings.add(Bindings.divide(source.layoutXProperty(), 2), Bindings.add(Bindings.divide(target.layoutXProperty(), 2), source.getWidth() / 2.0));
//Middlepoint Y source and target node
NumberBinding middleY = Bindings.add(Bindings.divide(source.layoutYProperty(), 2), Bindings.add(Bindings.divide(target.layoutYProperty(), 2), source.getWidth() / 2.0));
//X position of the anchorPane
//Middlepoint X - anchor.getWidth()/2
NumberBinding coordX = Bindings.subtract(middleX, anchor_link_label.getWidth()/2;
// Bounds bounds = anchor_link_label.getLayoutBounds();
// NumberBinding coordX = Bindings.subtract(middleX, (bounds.getMaxX() - bounds.getMinX()));
anchor_link_label.layoutXProperty().bind(coordX);
anchor_link_label.layoutYProperty().bind(middleY);
}
}
// FXML文件
<fx:root stylesheets="@application.css" type="Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<CubicCurve fx:id="node_link" controlX2="50.0" controlY1="10.0" controlY2="10.0" endX="10.0" fill="#1f93ff00" stroke="BLACK" />
<AnchorPane fx:id="anchor_link_label" managed="false">
<Label fx:id="label_link" text="" onMouseClicked="#showTextField" visible="false" managed="false"/>
<TextField fx:id="textField_link" minWidth="100" visible="true" managed="true"/>
</AnchorPane>
</children>
</fx:root>
在此代码运行时的高度和宽度是零,因为我猜节点尚未下岗了没有。
如果您希望middleX
, middleY
和coordX
绑定以当前的width / height更新,那么您需要将它们绑定到width height属性/绑定,而不只是在那一次调用时将width / height绑定,您可以进行“ source.getWidth() / 2.0
英寸(零)时。
尝试这个:
// Middlepoint X source and target node
NumberBinding middleX = source.layoutXProperty().divide(2)
.add(target.layoutXProperty().divide(2).add(source.widthProperty().divide(2)));
// Middlepoint Y source and target node
NumberBinding middleY = source.layoutYProperty().divide(2)
.add(target.layoutYProperty().divide(2).add(source.heightProperty().divide(2)));
// X position of the anchorPane
// Middlepoint X - anchor.getWidth()/2
NumberBinding coordX = middleX.subtract(anchor_link_label.widthProperty().divide(2));
// anchor_link_label.layoutXProperty().bind(coordX);
// anchor_link_label.layoutYProperty().bind(middleY);
// To avoid binding and getting a "bound value cannot be set" error
// you can just use listeners instead.
// However, this is slightly hackish, since you are re-laying-out this child
// after the parent tries to lay it out already.
// What you probably should do is define a parent that overrides
// `layoutChildren()` and determines how to layout this node.
// Here are the listeners:
coordX.addListener((obs, oldVal, newVal) -> {
anchor_link_label
.setLayoutX(
newVal.doubleValue() - anchor_link_label.getLayoutBounds().getMinX());
});
middleY.addListener((obs, oldVal, newVal) -> {
anchor_link_label
.setLayoutY(
newVal.doubleValue() - anchor_link_label.getLayoutBounds().getMinY());
});
一种可能的选择是使用HBox
作为父对象,并将对齐方式设置为居中,而不是使用通用Pane
,因为您似乎真正想要做的就是将标签放在窗格的中心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.