JavaFX菜单不会下拉

时间:2014-02-21 23:34:54

标签: java drop-down-menu javafx-2

我不知道我的菜单有什么问题,但是当我点击它时,它不会下拉以显示菜单项,例如“新建,打开,保存等”。我试过看其他例子,但我没有看到任何我可能错过的。

private MenuItem _fileNew = new MenuItem("New");
private MenuItem _fileOpen = new MenuItem("Open");
private MenuItem _fileSave = new MenuItem("Save");
private MenuItem _fileSaveAs = new MenuItem("Save as...");
private MenuItem _fileExit = new MenuItem("Exit");

private MenuItem _helpAbout = new MenuItem("About " + TITLE + "...");

@Override
public void start(Stage primaryStage) throws Exception
{
    Group root = new Group();
    MenuBar menuBar = new MenuBar();
    Menu menuFile = new Menu("FILE");

    _fileNew.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent actionEvent)
        {
            System.out.println("New Clicked");
        }
    });

    _fileOpen.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent actionEvent)
        {
            System.out.println("Open Clicked");
        }
    });

    _fileSave.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent actionEvent)
        {
            System.out.println("Save Clicked");
        }
    });

    _fileSaveAs.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent actionEvent)
        {
            System.out.println("Save As Clicked");
        }
    });

    _fileExit.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent actionEvent)
        {
            System.out.println("Exit Clicked");
        }
    });
    menuFile.getItems().addAll(_fileNew, _fileOpen, _fileSave, _fileSaveAs, _fileExit);

    // HELP MENU
    Menu menuHelp = new Menu("HELP");
    menuHelp.getItems().addAll(_helpAbout);

    menuBar.getMenus().addAll(menuFile, menuHelp);
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

    primaryStage.setTitle(TITLE + VERSION);
    primaryStage.setX(_bounds.getMinX());
    primaryStage.setY(_bounds.getMinY());
    primaryStage.setWidth(_bounds.getWidth());
    primaryStage.setHeight(_bounds.getHeight());

    root.getChildren().add(menuBar);
    root.getChildren().add(_canvas);

    primaryStage.setScene(new Scene(root));

    primaryStage.show();
}

1 个答案:

答案 0 :(得分:0)

使用BorderPane;将MenuBar放在顶部(borderPane.setTop(menuBar);),将其他内容放在中心。