JavaFX、SceneBuilder、MenuItem --> 图片

JavaFX, SceneBuilder, MenuItem --> Image

我最近发现了 google-material-图标,现在我想使用图标使我的应用程序更美观。现在我想将图像添加到应用程序关闭 MenuItem。使用 SceneBuilder 你可以只添加项目,但我现在想做的是至少仍然使用 fxml。我有两个问题:

  1. 我可以编辑 fxml 而不会再次被 scenebuilder 覆盖吗?
  2. 如何将图标添加到 MenuItems?

非常感谢。如果你需要它,这里是我的 FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../styles/Styles.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.freakyonline.ucone.FXMLController">
   <center>
      <TabPane prefHeight="167.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab closable="false" text="Players">
               <content>
                  <TableView fx:id="playerTable" editable="true" onContextMenuRequested="#handlePTContextMenuRequest" onInputMethodTextChanged="#handleTextChanged" prefHeight="200.0" prefWidth="200.0">
                    <columns>
                      <TableColumn fx:id="nickColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Nickname" />
                      <TableColumn fx:id="groupColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Group" />
                        <TableColumn fx:id="yearOfBirthColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="94.0" text="Year of Birth" />
                        <TableColumn fx:id="ageColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="72.0" text="Age" />
                        <TableColumn fx:id="genderColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="59.0" text="Gender" />
                        <TableColumn fx:id="lastQuitColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Last Quit" />
                    </columns>
                     <padding>
                        <Insets bottom="3.0" left="5.0" right="5.0" top="3.0" />
                     </padding>
                  </TableView>
               </content>
            </Tab>
          <Tab fx:id="consoleOneTab" closable="false" onSelectionChanged="#handleConsoleOneTabSelected" text="ConsoleOne">
               <content>
                  <VBox prefHeight="200.0" prefWidth="100.0">
                     <children>
                        <TextArea fx:id="consoleOneTextArea" editable="false" wrapText="true" VBox.vgrow="ALWAYS" />
                        <TextField fx:id="consoleOneTextField" alignment="TOP_LEFT" onAction="#handleConsoleOneAction" promptText="type here ..." />
                     </children>
                  </VBox>
               </content></Tab>
        </tabs>
      </TabPane>
   </center>
   <top>
      <MenuBar fx:id="mainMenuBar" BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#handleFileClose" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#handleHelpAbout" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

是的,您可以在 MenuItem 中添加 Image 而不会影响 fxml。

First, add FX:ID to your menu-item.

<MenuItem fx:id="close_item" mnemonicParsing="false" onAction="#handleFileClose" text="Close" />

Now use setGraphic() method to add an ImageView in your menu-item.

@FXML MenuItem close_item;

@Override
public void initialize(URL url, ResourceBundle rb) {

    ImageView menuIcon = new ImageView(new Image("/path/image.png"));
    menuIcon.setFitHeight(20);
    menuIcon.setFitWidth(20);
    close_item.setGraphic(menuIcon);
    //...
    //...
}