gpt4 book ai didi

JavaFX:getWidth() 和 getLayoutBounds() 返回 0

转载 作者:行者123 更新时间:2023-12-01 19:53:45 25 4
gpt4 key购买 nike

enter image description here

大家好,我正在开发一个简单的编辑器来使用 JavaFX 创建一些用例。现在,当我想将两个节点相互连接时,我想为该链接指定一个名称。目前的想法是将标签放入 anchor Pane 中,并将该 Pane 置于链接的中间。问题是, 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>

最佳答案

在这段代码运行时,高度和宽度为零,因为我猜测节点尚未布局。

如果您希望 middleXmiddleYcoordX 绑定(bind)更新为当前宽度/高度,那么您需要将它们绑定(bind)到宽度高度属性/绑定(bind),而不仅仅是您在零时进行“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,因为这似乎是您真正想要的它要做的就是将标签放在 Pane 的中央。

关于JavaFX:getWidth() 和 getLayoutBounds() 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com