简体   繁体   English

边框上的窗格上的JavaFX MenuBar(顶部)闪烁

[英]JavaFX MenuBar on Pane on Borderpane (top) flickers

I'm writing a program in Java and I'm using JavaFX for the GUI, using the MVC way. 我正在用Java编写程序,并且使用MVC方式将JavaFX用于GUI。

The structure of the view I'm talking about is as follow: 我正在谈论的视图结构如下:

查看结构

The reason for this structure is because I want to keep my code clean & clear. 之所以采用这种结构,是因为我想保持代码的整洁。

Inside the TopPane I want only a menuBar. 在TopPane内部,我只需要一个menuBar。

I've added this as follow: 我添加了以下内容:

public class TopPane extends Pane {
    private MenuBar menuBar;

    public TopPane() {
        initNodes();
        layoutNodes();
    }

   private void initNodes() {
        Menu[] menus = new Menu[3];
        menus[0] = new Menu("File");
        menus[1] = new Menu("Options");
        menus[2] = new Menu("Help");

        MenuItem[] menuItemsFile = new MenuItem[4];
        menuItemsFile[0] = new MenuItem("New game");
        menuItemsFile[1] = new MenuItem("Save game");
        menuItemsFile[2] = new MenuItem("Load game");
        menuItemsFile[3] = new MenuItem("Exit");
        menus[0].getItems().addAll(menuItemsFile);

        MenuItem options = new MenuItem("Options");
        menus[1].getItems().add(options);

        MenuItem help = new MenuItem("Help");
        menus[2].getItems().add(help);

        menuBar = new MenuBar();
        menuBar.getMenus().addAll(menus);
    }

    private void layoutNodes() {
        menuBar.setMinWidth(Double.MAX_VALUE);
        menuBar.useSystemMenuBarProperty().set(true);
        getChildren().add(menuBar);
    }

    public MenuBar getMenuBar() {
        return menuBar;
    }
}

In gameview I do the following: 在gameview中,我执行以下操作:

public class GameView extends BorderPane {
    private TopPane topPane;
    private LeftPane leftPane;
    private PlayerView playerView; //centerpane
    private RightPane rightPane;

    public GameView() {
        initNodes();
        layoutNodes();
    }

    private void initNodes() {
        topPane = new TopPane();
        leftPane = new LeftPane();
        playerView = new PlayerView();
        rightPane = new RightPane();
    }

    private void layoutNodes() {
        this.setStyle("-fx-background-color:  #2a2a2a");

        this.setTop(topPane);

        Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
        double screenHeight = screenBounds.getHeight();
        int topMargin = 25;
        if (screenHeight >= 1050) {
            topMargin = 150;
        }
        System.out.println(screenHeight);

        setMargin(leftPane, new Insets(topMargin, 0, 0, 50));
        this.setLeft(leftPane);

        setMargin(playerView, new Insets(0, 0, 0, 25));
        this.setCenter(playerView);

        this.setRight(rightPane);
    }
}

It shows the menubar succesfully but the problem is that when I click one of the menus, the menuItems just flickers and dissapear immediatly. 它成功显示了菜单栏,但是问题是,当我单击菜单之一时,menuItems会立即闪烁并消失。 I think this is because I extend topPane with Pane. 我认为这是因为我使用Pane扩展了topPane。 But without it couldn't work obviously, and if I extend from menuBar (and delete the variable Menubar), my rightPane & PlayerView won't be added to my GameView for some mysterious dark reason I couldn't figure out (yet) :) 但是如果没有它,就无法正常工作,并且如果我从menuBar扩展(并删除变量Menubar),由于某些我无法弄清的神秘黑暗原因,我的rightPane和PlayerView将不会添加到我的GameView中: )

Anybody able to help me? 有人可以帮助我吗? Thanks in advance! 提前致谢!

Thanks to DVarga : 感谢DVarga:

  • Remove menuBar.setMinWidth(Double.MAX_VALUE); 删除menuBar.setMinWidth(Double.MAX_VALUE);
  • Extend StackPane instead of Pane 扩展StackPane而不是窗格

This works perfectly! 这样完美!

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

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