如何在菜单栏上方显示文字 - java fx

时间:2017-11-07 14:17:03

标签: java javafx javafx-8

我正在尝试做这样的事情 Image to what I mean

我尝试过这个问题Text on java fx menu,但没有任何效果,我最终得到了my version。我也不确定如何使其响应,所以如果它的尺寸更大,它将改变以适应屏幕尺寸。 (JavaFX新手)

这是代码

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>

<!--<AnchorPane id="menu-bar-container" stylesheets="@TestCSS.css" xmlns="http://javafx.com/javafx/8.0.111">-->
    <AnchorPane xmlns:fx="http://javafx.com/fxml/1" styleClass="menu-bar" id="menu-bar-container" stylesheets="@TestCSS.css">
    <MenuBar id="menu-bar" AnchorPane.leftAnchor="0" AnchorPane.topAnchor="0">
        <Menu text="File">
            <items>
                <MenuItem text="Close" />
            </items>
        </Menu>
        <Menu text="Help">
            <items>
                <MenuItem text="About" />
            </items>
        </Menu>
    </MenuBar>
    <Label style="-fx-font-weight: bold; -fx-text-fill: black; " text="Some text" AnchorPane.bottomAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" />
</AnchorPane>

我的样式代码

    #menu-bar {
    -fx-padding: 0 ;
    -fx-background-color: transparent ;
    -fx-background-insets: 0 ;
    -fx-background-radius: 0 ;
}

#menu-bar-container {
    -fx-padding: 0.0em 0.666667em 0.0em 0.666667em; /* 0 8 0 8 */
    -fx-background-color:
        linear-gradient(to bottom, derive(-fx-base,75%) 0%, -fx-outer-border 90%),
        linear-gradient(to bottom, derive(-fx-base,46.9%) 2%, derive(-fx-base,-2.1%) 95%);
    -fx-background-insets: 0 0 0 0, 1 0 1 0;
    -fx-background-radius: 0, 0 ;
    -fx-background-color: red ;
}

1 个答案:

答案 0 :(得分:1)

此示例使用StackPane MenuBarLabel

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <StackPane>
               <children>
                  <MenuBar>
                    <menus>
                      <Menu mnemonicParsing="false" text="File">
                        <items>
                          <MenuItem mnemonicParsing="false" text="Close" />
                        </items>
                      </Menu>
                      <Menu mnemonicParsing="false" text="Edit">
                        <items>
                          <MenuItem mnemonicParsing="false" text="Delete" />
                        </items>
                      </Menu>
                      <Menu mnemonicParsing="false" text="Help">
                        <items>
                          <MenuItem mnemonicParsing="false" text="About" />
                        </items>
                      </Menu>
                    </menus>
                  </MenuBar>
                  <Label fx:id="lblUsername" text="Label" StackPane.alignment="CENTER_RIGHT">
                     <StackPane.margin>
                        <Insets right="10.0" />
                     </StackPane.margin>
                  </Label>
               </children>
            </StackPane>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" VBox.vgrow="ALWAYS">
               <children>
                  <Label layoutX="258.0" layoutY="159.0" text="App Goes Here!" />
               </children>
            </AnchorPane>
            <ToolBar prefHeight="40.0" prefWidth="200.0">
              <items>
                <Button mnemonicParsing="false" text="Bread Crumb Bar usually goes here" />
              </items>
            </ToolBar>
         </children>
      </VBox>
   </children>
</AnchorPane>

enter image description here

相关问题