简体   繁体   English

JAVA FX:如何在 fxml 视图的同一场景中设置菜单栏

[英]JAVA FX : How to set menubar in same scene of fxml view

I wouldlke to add my menubar from my borderpane but the problem actually is I loaded my view (FXML) on primary stage.我想从我的边框添加我的菜单栏,但问题实际上是我在初级阶段加载了我的视图 (FXML)。 I wouldike to add my menubar at top of my view :我想在我的视图顶部添加我的菜单栏:

Parent root = FXMLLoader.load(getClass().getResource("/view/home/home.fxml"));

// Create MenuBar
MenuBar menuBar = new MenuBar();

// Create menus
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
Menu helpMenu = new Menu("Help");

BorderPane rootTop = new BorderPane();
rootTop.setTop(menuBar);

primaryStage.setScene(new Scene(root, 900, 600));
primaryStage.setScene(new Scene(rootTop, 900, 600));

primaryStage.show();

This code:这段代码:

primaryStage.setScene(new Scene(root, 900, 600));
primaryStage.setScene(new Scene(rootTop, 900, 600));

sets the scene two times.设置场景两次。 The second Scene overrides the first one, so you won't be able to see your loaded FXML.第二个Scene覆盖第一个Scene ,因此您将无法看到加载的 FXML。

What you can do is embed your FXML in your BorderPane :您可以做的是您的 FXML嵌入您的BorderPane

BorderPane rootTop = new BorderPane();
rootTop.setTop(menuBar);
rootTop.setCenter(root);

primaryStage.setScene(new Scene(rootTop, 900, 600));

primaryStage.show();

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

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