简体   繁体   English

JavaFX MenuButton 仅作为具有 MenuButton 功能的图形或图标

[英]JavaFX MenuButton only as a Graphic or an Icon with the functionality of a MenuButton

菜单按钮

I added a MenuButton and assigned an icon for its graphic.我添加了一个MenuButton并为其图形分配了一个图标。 Whenever I hover over the graphic, the color changes from black to blue.每当我将鼠标悬停在图形上时,颜色就会从黑色变为蓝色。 The problem is, I don't need to precisely hover over the graphic in order to open the menu.问题是,我不需要精确地将鼠标悬停在图形上以打开菜单。 If I were to click on any of the places marked with the red dots the options appear below.如果我点击任何标有红点的地方,选项就会出现在下面。

I've looked around and managed to remove as many things as I could find using the following CSS :我环顾四周,设法使用以下CSS删除了尽可能多的东西:

#menubutton {
    -fx-background-color: transparent;
    -fx-background-insets: 0 0 0 0;
    -fx-border-color: transparent;
    -fx-padding: 0;
}
#menubutton > .arrow-button {
    -fx-background-color: transparent;
    -fx-padding: 0;
}
#menubutton > .arrow-button > .arrow {
    -fx-background-color: transparent;
    -fx-padding: 0;
}

But if clicking on those places stills opens the menu options, then it seems like there's still some space between the icon and the border of the button.但是如果点击这些地方仍然打开菜单选项,那么图标和按钮边框之间似乎还有一些空间。 In this example you can see the space more clearly. 在此示例中,您可以更清楚地看到空间。

Is there a way to either remove that space or give an icon the functionality of a MenuButton without the button itself?有没有办法删除那个空间或者给一个图标一个没有按钮本身的MenuButton的功能?

PS The other icons work fine because they're by themselves and not as a graphic in a button. PS 其他图标工作正常,因为它们是单独的,而不是按钮中的图形。

You need to also turn off the padding to the label within the menu button.您还需要关闭菜单按钮内标签的填充。

#menubutton > .arrow-button,
#menubutton > .label{
    -fx-background-color: transparent;
    -fx-padding: 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM