css - java fx 菜单栏上的文本

标签 css javafx fxml

我正在使用 JavaFx 和 fxml 文件来编写应用程序以供查看。如何在菜单右侧添加文本(如附图所示)。场景生成器允许我仅添加菜单,但文本位于左侧(白色框所在的位置),我必须禁用此菜单(因为它不是菜单)。 我也想改变这个菜单的风格,但是不行。只能有一个菜单吗?

我的fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>

<MenuBar xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.controler.MainMenuController">
    <menus>
        <Menu fx:id="fileMenu" mnemonicParsing="false" text="%file">
            <items>
                <MenuItem fx:id="closeMenuItem" mnemonicParsing="false" text="%close" />
            </items>
        </Menu>
        <Menu fx:id="menuHelp" mnemonicParsing="false" text="%help">
            <items>
                <MenuItem fx:id="aboutMenuItem" mnemonicParsing="false" text="%about.title" />
            </items>
        </Menu>
      <Menu fx:id="loginName" mnemonicParsing="false" style="font-weight: bold; color: black;">
        <items>
          <MenuItem mnemonicParsing="false" text="Action 1" />
        </items>
      </Menu>
    </menus>
</MenuBar>

附件:

enter image description here

最佳答案

如果您只想在右端显示文本,则可以使用 Label 。包裹MenuBarLabel例如,AnchorPane获得所需的布局:

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

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

<AnchorPane xmlns:fx="http://javafx.com/fxml/1">
    <MenuBar 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 text="Some text" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" AnchorPane.bottomAnchor="0" 
        style="-fx-font-weight: bold; -fx-text-fill: black; " />
</AnchorPane>

这给出了以下内容:

enter image description here

您可能想尝试使用一些 CSS 来获得您想要的样式,但这会按照您需要的方式定位所有内容。一种非常快速的样式设置方法,以便 AnchorPane样式就像菜单栏一样只是添加 styleClass="menu-bar"AnchorPane元素:

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" styleClass="menu-bar">

现在给出

enter image description here

对于(也许)更强大的方法,请添加 id="menu-bar-container"AnchorPaneid="menu-bar"到菜单栏。然后以下外部 CSS 会将默认菜单栏样式移动到 anchor Pane :

#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 ;
}

关于css - java fx 菜单栏上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576445/

相关文章:

javascript - 将多个 javascript 表单合并为一个带有选择选项的表单

JavaFX 如何在 GridPane 中的 HBox 中垂直对齐复选框

java - 如何初始化自定义 javafx Controller 的成员变量?

java - 在同一窗口中打开另一个 GridPane

css - 如何在所有主要网络浏览器上支持 border-radius?

html - 防止背景布局拉伸(stretch)

pagination - 如何在JavaFX中实现TableView的分页?

javafx - 如何在JavaFX中使用PauseTransition方法?

JavaFX 错误日志 - 调用目标异常

css - LESS CSS 使用不同的前缀转义整个 CSS 规则?