java - 如何在菜单栏顶部获取文本 - javafx

标签 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 ;
}

最佳答案

此示例使用带有 MenuBarLabelStackPane

<?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

关于java - 如何在菜单栏顶部获取文本 - javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160139/

相关文章:

java - JavaFX Pane 对象数组?

javafx-8 - JavaFX 加载 FXML 的性能问题

javafx - 是否可以自动更新 native JavaFX 应用程序?

java - JNI 检测到应用程序错误 : use of deleted local reference 0x1

python - 如何解决此错误 : Py4JJavaError: An error occurred while calling o70. showString?

java - CyclicBarrier 浪费时间

java - 如何在JavaFX中绑定(bind)到复合属性的组件?

JavaFX setCellValueFactory

java - 多线程

java - 如何右对齐 Java FX 工具栏中的按钮