JavaFx:如何在FXML中设置Label的labelFor属性?

时间:2017-08-16 08:47:31

标签: javafx fxml

我有一个TextField和一个mnemonicParsing为true的标签。我想设置标签的labelFor属性与TextField的id。我如何在FXML中做到这一点?

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
      <Label maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true" />
      <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
</children>
</GridPane>

由于

2 个答案:

答案 0 :(得分:4)

您可以使用美元符号:

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
          <Label labelFor="$myTextField" maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true"  />
          <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
    </children>
</GridPane>

答案 1 :(得分:1)

如果找到解决方案......

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
      <Label maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true">
           <labelFor>
               <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
           </labelFor>
      </Label>
      <fx:reference source="myTextField" />
</children>
</GridPane>