【问题标题】:Inclusion of tags in FXML在 FXML 中包含标签
【发布时间】:2012-08-21 09:52:26
【问题描述】:

我想知道是否有办法包含在同一个 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>

我已阅读此答案https://stackoverflow.com/a/8490764/1606953,但它涵盖了包含外部源(=其他文件)。我对只包含一小段代码更感兴趣

谢谢大家

【问题讨论】:

    标签: javafx-2 fxml


    【解决方案1】:

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

    【讨论】:

    • 我已经回复了选项 + ,这很适合这里,但它只接受 1 个参考!感谢您的回复!
    【解决方案2】:

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

    我的-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 个参考时才有效。如果我们进行 3 次引用,就像这里一样,我们会得到这个异常:

    Children: duplicate children added: parent = HBox@2df02fde
    file:/C:/.../my-tab.fxml:48
    file:/C:/.../Xxx.jar!/xxx/client/MainTabs.fxml:16
      at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
      at com.sun.javafx.collections.VetoableObservableList.add(VetoableObservableList.java:165)
      at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
      at javafx.fxml.FXMLLoader$Element.add(FXMLLoader.java:130)
      at javafx.fxml.FXMLLoader$PropertyElement.add(FXMLLoader.java:1179)
      at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:609)
      at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
      at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:937)
      at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:567)
      at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
      at xxx.client.XXX.start(XXX.java:21)
      at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
      at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
      at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
      at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:67)
      at java.lang.Thread.run(Thread.java:722)  
    

    有什么选择吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2017-04-06
      • 1970-01-01
      相关资源
      最近更新 更多