JavaFX 如何让菜单栏仅在宽度上适合父级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19341390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX how to get menubar to fit to parent in width only?
提问by j will
I am using Scene Builder to create my GUI and my menu bar isn't resizing to the parent. How do i get it to do this?
我正在使用 Scene Builder 创建我的 GUI,我的菜单栏没有调整到父级。我如何让它做到这一点?
When I try Modify -> Fit to Parent, the menu bar takes up the whole root pane, and obviously this isn't what i want. Here is an image of what i am talking about:
当我尝试 Modify -> Fit to Parent 时,菜单栏占据了整个根窗格,显然这不是我想要的。这是我正在谈论的图像:
As you can see, the menu bar doesn't fully extend to the end of the window.
如您所见,菜单栏没有完全延伸到窗口的末尾。
采纳答案by j will
I had to set the anchor pane constraints in the menu bar. I had to make the left and right margins zero:
我必须在菜单栏中设置锚窗格约束。我必须使左右边距为零:
回答by lochi
Here is another way to do it. Wrap MenuBar in a HBOX. Set MenuBar's HGrow to ALWAYS. Set HBOX Node Alignment to TOPLEFT.
这是另一种方法。将 MenuBar 包裹在 HBOX 中。将 MenuBar 的 HGrow 设置为 ALWAYS。将 HBOX 节点对齐设置为 TOPLEFT。
回答by Juan Carlos Sanchez
You need to bind the preferred width property of your menu bar to the width
of your stage or some other container node. It works when you dynamically resize too.
您需要将菜单栏的首选宽度属性绑定到width
舞台或其他容器节点的 。它也适用于动态调整大小。
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());