JavaFX,SceneBuilder,MenuItem - >图片

时间:2017-03-05 06:24:56

标签: javafx javafx-8 fxml menuitem scenebuilder

我最近发现了google-material-icons,现在我希望使用图标让我的应用程序看起来更好看。现在我想在关闭MenuItem的应用程序中添加一个图像。使用SceneBuilder你可以添加项目,但我现在想做的是至少仍然使用fxml。我有两个问题:

  1. 我可以编辑fxml而不会再被scenebuilder覆盖吗?
  2. 如何将图标添加到MenuItems?
  3. 非常感谢你。万一你需要它,这里我的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>
    

1 个答案:

答案 0 :(得分:0)

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

  

首先,将FX:ID添加到您的菜单项。

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

现在使用setGraphic()方法在菜单项中添加ImageView

@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);
    //...
    //...
}