我试图在控制器类中获得文本字段的X,Y位置,我已经包装在一个hbox中,还有一个标签和组合框。
我知道通常可以通过调用<code>layoutX()</code>和<code>layoutY()</code>获得X、Y,但它返回了0。
以下是fxml代码
<HBox alignment="CENTER_LEFT" padding="$x1" prefHeight="-1.0" prefWidth="-1.0" spacing="10.0">
<children>
<Label maxWidth="-Infinity" minWidth="-Infinity" prefWidth="105.0" text="Add a Field:" />
<TextField fx:id="addFieldName" prefWidth="200.0" promptText="Field Name" />
<ComboBox fx:id="addFieldTypeComboBox" prefWidth="180.0" promptText="Choose a field type...">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
<Button fx:id="addFieldButton" mnemonicParsing="false" onAction="#handleAddFieldButtonAction" text="Add" />
</children>
</HBox>`
我甚至尝试get < code > add field name . getwidth()也返回0。
Node
的几个属性可以让你获得其边界的详细信息。
比如 getBoundsInLocal() 和 getBoundsInParent()
;
请参考getBoundsInLocal。