2016-09-27 67 views
1

我已经在FXML中用一堆菜单项(包含图形,onClick方法链接等)声明了菜单栏。为菜单栏和上下文菜单重用相同的FXML声明

现在我正在创建一个表格的上下文菜单,并且我想在那里放置菜单栏“编辑”菜单中的所有菜单项。

FXML中有这样做的干吗?

我不喜欢复制菜单项的所有FXML声明并且不得不维护两组项目的想法。 我知道我可以重复使用这些项目,如果我在Java代码中声明它们,但我想将所有布局保留在FXML中。

下面是编辑菜单中的FXML,我不想重复:

<Menu text="_Edit"> 
    <MenuItem onAction="#copyRaw" text="Copy _raw log"> 
     <accelerator> 
      <KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="UP" shortcut="UP" /> 
     </accelerator> 
     <graphic> 
      <Glyph fontFamily="FontAwesome" icon="copy" /> 
     </graphic> 
    </MenuItem> 
    <MenuItem onAction="#copyPretty" text="Copy with _columns"> 
     <accelerator> 
      <KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="DOWN" shortcut="UP" /> 
     </accelerator> 
     <graphic> 
      <Glyph fontFamily="FontAwesome" icon="copy" /> 
     </graphic> 
    </MenuItem> 
    <SeparatorMenuItem mnemonicParsing="false" /> 
    <MenuItem onAction="#selectAll" text="Select _All"> 
     <accelerator> 
      <KeyCodeCombination alt="UP" code="A" control="DOWN" meta="UP" shift="UP" shortcut="UP" /> 
     </accelerator> 
    </MenuItem> 
    <MenuItem mnemonicParsing="false" onAction="#unselectAll" text="Unselect All" /> 
</Menu> 

回答

0

我不知道我的想法是最好的,但ü应该做这样的: 您可以在资源中使用大多数使用过的元素(如按钮,表格,标签等)创建一个包,每个元素分配一个fx:id并将其包含在另一个fxmls中。 实施例:

PACKAGE:资源/包/名称/ utils的/ Label.fxml

<?import javafx.scene.control.Label?> 
<?import javafx.geometry.Insets?> 
<Label xmlns:fx="http://javafx.com/fxml" 
    fx:id="label" 
    style="-fx-background-color: red; 
      -fx-font: bold; 
      -fx-font-size: 30px;" 
    text="Hello world"> 
<padding> 
    <Insets top="5" left="5" right="5" bottom="5"/> 
</padding> 
</Label> 
<!--Add all your nodes there--> 

PACKAGE:资源/包/名称/ Main.fxml

<GridPane xmlns:fx="http://javafx.com/fxml" alignment="TOP_CENTER" hgap="10" vgap="10"> 
<fx:include source="Label.fxml"/> <!--There will be displayed all elements from Label.fxml--> 
</GridPane> 

在你的情况下,你只需要设置FX:ID为您的菜单项和导入其他fxml。 祝你好运。

+0

谢谢,但我不想在这里重复使用自定义组件,我试图重新使用绑定到控制器方法的整个结构,所以它比这个例子稍微复杂一些。 – Joffrey