将菜单栏添加到网格窗格

Adding Menubar to Gridpane

如何将 MenuBar 添加到舞台?我已经尝试将它包含在我的 GridPane 中(我已将所有其他元素添加到其中),但它看起来从来没有附加到 window.

的顶部

有办法解决吗?

我使用这段代码将其放置在布局中。

 layout.add(menuBar,0,1,10,1);

其中 layoutGridPanemenuBar 是添加了菜单内容的 MenuBar

如果您在代码中执行此操作,请尝试使用更好的节点作为 root。当我有 MenuBar 时,我喜欢使用 VBox。您可以在 MenuBar 之后添加您的 GridPane

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication249 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        Menu miFile = new Menu("File");
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().add(miFile);

        VBox root = new VBox(menuBar);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}