【问题标题】:Adding Menubar to Gridpane将菜单栏添加到 Gridpane
【发布时间】:2019-02-02 08:11:36
【问题描述】:

如何将MenuBar 添加到阶段?我已经尝试将它包含在我的GridPane(我已将所有其他元素添加到其中)中,但它看起来从来没有像附加到窗口顶部一样。

有没有办法解决这个问题?

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

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

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

【问题讨论】:

  • 不要将MenuBar 添加到GridPane
  • GridPaneMenuBar 包装在不同的布局中,例如BorderPane

标签: java user-interface javafx menubar gridpane


【解决方案1】:

如果您在代码中执行此操作,请尝试使用更好的节点作为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);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多