Javafx 2 在FXML中包含标记

Javafx 2 在FXML中包含标记,javafx-2,fxml,Javafx 2,Fxml,我想知道是否有一种方法可以包含在同一个FXML文件中定义的代码(标记)。我想要的是多次包含相同的图像,而不必复制粘贴所有属性: <HBox> <Label translateY="25" alignment="center" text=""> <graphic> <ImageView fitWidth="100" preserveRatio="true" smooth="true"> <image&g

我想知道是否有一种方法可以包含在同一个FXML文件中定义的代码(标记)。我想要的是多次包含相同的图像,而不必复制粘贴所有属性:

<HBox>
  <Label translateY="25" alignment="center"  text="">
    <graphic>
      <ImageView fitWidth="100" preserveRatio="true" smooth="true">
        <image>
          <Image url="arrow_right.png"/>
        </image>
      </ImageView>
    </graphic>
  </Label>
</HBox>

我想要一些像:

<fx:define>
  <Label fx:id="myLabel" translateY="25" alignment="center"  text="">
    <graphic>
      <ImageView fitWidth="100" preserveRatio="true" smooth="true">
        <image>
          <Image url="arrow_right.png"/>
        </image>
      </ImageView>
    </graphic>
  </Label>
</fx:define>

<HBox>
    <fx:include source="$myLabel" />
</HBox>

我读过这个答案,但它包含了外部源(=其他文件)。我更感兴趣的是只包含一小段代码

谢谢大家

绝对是最好的处理方法。您不能在此处使用,因为“myLabel”只能有一个父节点。通过将标签放置在包含中,您将能够创建内容的多个实例,每个实例都可以有自己的父实例。

无疑是处理此问题的最佳方法。您不能在此处使用,因为“myLabel”只能有一个父节点。通过将标签放置在包含中,您将能够创建内容的多个实例,每个实例都可以有自己的父实例。

我(几乎)找到了我要查找的内容:

my-tab.fxml:

<HBox>
  <fx:define>
    <Label fx:id="arrow" alignment="center"  text="" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS">
     <graphic>
        <ImageView fitWidth="100" preserveRatio="true" smooth="true">
           <image>
             <Image url="peet/resources/images/arrow_right.png"/>
           </image>
        </ImageView>
     </graphic>
    </Label>
  </fx:define>
  <children>
    <Button wrapText="true" text="AAA" maxHeight="Infinity" maxWidth="Infinity"  VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />                              
    <Button text="BBB" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button text="CCC" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button  text="DDD" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
  </children>       
</HBox> 
有什么选择吗?

我(几乎)找到了我要找的东西:

my-tab.fxml:

<HBox>
  <fx:define>
    <Label fx:id="arrow" alignment="center"  text="" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS">
     <graphic>
        <ImageView fitWidth="100" preserveRatio="true" smooth="true">
           <image>
             <Image url="peet/resources/images/arrow_right.png"/>
           </image>
        </ImageView>
     </graphic>
    </Label>
  </fx:define>
  <children>
    <Button wrapText="true" text="AAA" maxHeight="Infinity" maxWidth="Infinity"  VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />                              
    <Button text="BBB" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button text="CCC" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button  text="DDD" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
  </children>       
</HBox> 

有任何选项吗?

我已经用选项+,这将非常适合这里,但它只接受1个参考!谢谢你的回复!我已经回答了选项+,这将非常适合这里,但它只接受1个参考!谢谢你的回复!