简体   繁体   English

向以JavaFX为中心的网格窗格添加顶部菜单

[英]Add a top menu to JavaFX centered Grid Pane

I'm very new to JavaFX. 我对JavaFX非常陌生。 I am using a grid pane to keep my items centered on the page regardless of how the window is resized. 我正在使用网格窗格将我的项目保持在页面中央,而不管窗口如何调整大小。 I want to add a menu that runs along the top. 我想添加一个沿着顶部运行的菜单。 I quickly found out grid.setTop(menuBar) is not a member function of grid pane. 我很快发现grid.setTop(menuBar)不是网格窗格的成员函数。 Is there a way to this? 有办法吗?

Can I create two different types of panes in one scene? 我可以在一个场景中创建两种不同类型的窗格吗? IE, a GridPane to center items and a BorderPane to get the menu up top? IE,用于居中放置项目的GridPane和用于将菜单置于顶部的BorderPane? Or should I use CSS styling to get the menubar at the top? 还是应该使用CSS样式将菜单栏置于顶部?

Here is some code I'm using: 这是我正在使用的一些代码:

public void start(Stage primaryStage) {
    try {
    primaryStage.setTitle("Bapu Inventory");
    BorderPane root = new BorderPane();
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));
    Text scenetitle = new Text("Welcome");
    grid.add(scenetitle, 0, 0, 1, 1);


    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
//This is the line I can't figure out. How do I get this to position at top left?
    grid.setTop(menuBar);

Any help would be greatly appreciated here. 任何帮助将不胜感激在这里。 I looked through the documentation Oracle provides but didn't find this feature listed anywhere. 我浏览了Oracle提供的文档,但未在任何地方找到此功能。

Check if this is what you want: 检查这是否是您想要的:

public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();

            //Create your menu
            final Menu menu1 = new Menu("File");
            final Menu menu2 = new Menu("Options");
            final Menu menu3 = new Menu("Help");
            MenuBar menuBar = new MenuBar();
            menuBar.getMenus().addAll(menu1, menu2, menu3);

            //Your GridPane
            GridPane grid = new GridPane();
            grid.setAlignment(Pos.CENTER);
            grid.setHgap(10);
            grid.setVgap(10);
            grid.setPadding(new Insets(25, 25, 25, 25));
            Text scenetitle = new Text("Welcome");
            grid.add(scenetitle, 0, 0, 1, 1);

            //Add them  to root (BorderPane)
            root.setTop(menuBar);
            root.setCenter(grid);


            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
}

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

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