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

时间:2018-05-15 15:07:23

标签: java javafx layout fxml

enter image description here

大家好, 我正在开发一个简单的编辑器来使用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>

1 个答案:

答案 0 :(得分:3)

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

如果您想要使用当前宽度/高度更新middleXmiddleYcoordX绑定,则需要将它们绑定到宽度高度属性/绑定而不是仅仅你拨打的那个电话的宽度/高度&#34; source.getWidth() / 2.0&#34;当它为零时。

试试这个:

// 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,因为您似乎真正希望它做的就是放置窗格中央的标签。