JavaFX:在 BorderPane 中时,菜单项仅显示为三个点
JavaFX: Menu items only shown as three dots when in BorderPane
我正在尝试学习 JavaFX,但我 运行 遇到 MenuBar
中 Menus
的问题。这是一个最小的例子:
public void start(Stage mainStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 1200, 1000, Color.WHITE);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("_File");
menuBar.getMenus().add(menuFile);
MenuItem add = new MenuItem("_New");
menuFile.getItems().add(add);
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(mainStage.widthProperty());
mainStage.setScene(scene);
mainStage.show();
}
此应用程序启动,但 MenuBar
中的 Menu
仅显示为三个点 (...)。但是当我按下 ALT+F
时它确实打开了,所以它就在那里。
据我了解,Menu
项目没有 width
或类似属性,因此无法设置。我怀疑这与我的根节点是 BorderPane
有关,因为在我发现的每个其他示例中,根节点是 VBox
或其他。当我将 Vbox
作为我的根节点,然后将 MenuBar
和 BorderPane` 添加到根节点时,我似乎得到了想要的结果——但这对我来说似乎是一个奇怪且不必要的解决方法。
那么我在这里缺少什么? MenuBar
真的只会在某些容器中看起来像它应该的样子吗? BorderPane
和 VBox
在这方面有什么区别?如果有人可以解释或指出我错过的文档的一部分,我将不胜感激。
您正在使用 BorderPane
并使用 getChildren().add()
在其中添加 MenuBar,这是不正确的。 BorderPane
不像VBox
,不能接受任意数量的children,分为5个具体位置:
- 顶部
- 左
- 对
- 底部
- 居中
children 进入这些位置中的任何一个。 Please go through the documentation for BorderPane.
您需要使用以下方法将菜单栏添加到 BorderPane 的顶部:
root.setTop(menuBar);
我正在尝试学习 JavaFX,但我 运行 遇到 MenuBar
中 Menus
的问题。这是一个最小的例子:
public void start(Stage mainStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 1200, 1000, Color.WHITE);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("_File");
menuBar.getMenus().add(menuFile);
MenuItem add = new MenuItem("_New");
menuFile.getItems().add(add);
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(mainStage.widthProperty());
mainStage.setScene(scene);
mainStage.show();
}
此应用程序启动,但 MenuBar
中的 Menu
仅显示为三个点 (...)。但是当我按下 ALT+F
时它确实打开了,所以它就在那里。
据我了解,Menu
项目没有 width
或类似属性,因此无法设置。我怀疑这与我的根节点是 BorderPane
有关,因为在我发现的每个其他示例中,根节点是 VBox
或其他。当我将 Vbox
作为我的根节点,然后将 MenuBar
和 BorderPane` 添加到根节点时,我似乎得到了想要的结果——但这对我来说似乎是一个奇怪且不必要的解决方法。
那么我在这里缺少什么? MenuBar
真的只会在某些容器中看起来像它应该的样子吗? BorderPane
和 VBox
在这方面有什么区别?如果有人可以解释或指出我错过的文档的一部分,我将不胜感激。
您正在使用 BorderPane
并使用 getChildren().add()
在其中添加 MenuBar,这是不正确的。 BorderPane
不像VBox
,不能接受任意数量的children,分为5个具体位置:
- 顶部
- 左
- 对
- 底部
- 居中
children 进入这些位置中的任何一个。 Please go through the documentation for BorderPane.
您需要使用以下方法将菜单栏添加到 BorderPane 的顶部:
root.setTop(menuBar);