簡體   English   中英

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

[英]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>

在此代碼運行時的高度和寬度零,因為我猜節點尚未下崗了沒有。

如果您希望middleXmiddleYcoordX綁定以當前的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM