java fx菜单栏上的文本 [英] Text on java fx menu bar

查看:218
本文介绍了java fx菜单栏上的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing app using JavaFx with fxml file for view. How can I add text on right side of menu (like on attached picture). Scene Builder allows me to add only Menu, but then the text is on the left (where the white box is) and I must disable this menu (because it is not menu). I also wanted to change the style of this menu, but it is not working. It is possible for only one menu?.

我的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>

附件:

推荐答案

如果您只需要右侧的文字,则可以使用标签。包含 MenuBar 标签,例如 AnchorPane 获得所需的布局:

If you want just text at the right end, you can use a Label. Wrap the MenuBar and the Label in, for example, an AnchorPane to get the desired layout:

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

这给出以下内容:

< a href =https://i.stack.imgur.com/lIvEi.png =nofollow noreferrer>

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

You may want to experiment with some CSS to get the style the way you want, but this will position everything the way you need it. A very quick way to style it so that the AnchorPane is styled like the menu bar is just to add styleClass="menu-bar" to the AnchorPane element:

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

现在给出

对于(可能)更强大的方法,添加 id =menu-bar-container到菜单栏的 AnchorPane id =menu-bar。然后,以下外部CSS将默认菜单栏样式移动到锚点窗格:

For (maybe) a more robust approach, add id="menu-bar-container" to the AnchorPane and id="menu-bar" to the menu bar. Then the following external CSS will move the default menu bar styles to the 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 ;
}

这篇关于java fx菜单栏上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆