简体   繁体   English

如何在顶部显示MenuBar的多个anchorPanes之间切换?

[英]How to switch between multiple anchorPanes with MenuBar on top?

I'm new to JavaFX and I need to switch between multiple Anchor Panes using a MenuBar , so the MenuBar needs to stay on top when I'm switching between these Anchor Panes, I'm using Scene Builder to generate the .FXML files, but have no idea how to do this without creating multiple MenuBars for each Anchor Pane. 我是JavaFX新手,我需要使用MenuBar在多个Anchor Panes之间切换,因此,当我在这些锚定窗格之间进行切换时, MenuBar需要保持在顶部,我正在使用Scene Builder生成.FXML文件,但不知道如何在不为每个锚定窗格创建多个MenuBars情况下执行此操作。

My question is how to do this in a simple way? 我的问题是如何以简单的方式做到这一点? Because I have ready to use Anchor Panes, all I need is a way to share the Menu bar on top of these Anchor panes. 因为我已经准备好使用锚定窗格,所以我所需要的就是共享这些锚定窗格顶部的菜单栏的方法。

As James Commented, to do this you need to use borderPane with the MenuBar on top of it, and set your Anchor panes inside it using the setCenter() method on the BoderPane , like this: 正如詹姆斯评价说,要做到这一点,你需要使用borderPane与MenuBar在它的上面,并用设置里面的锚窗格setCenter()的方法BoderPane ,就像这样:

    @FXML
    private BorderPane borderPane;

    private AnchorPane achor1;
    private AnchorPane achor2;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            // TODO
            achor1 = FXMLLoader.load(getClass().getResource(("/Views/anchor1.fxml")));
            achor2 = FXMLLoader.load(getClass().getResource("/Views/anchor2.fxml"));

        } catch (IOException ex) {
            Logger.getLogger(CrechHomeController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @FXML
    public void action1() throws IOException {
        // anchor1

        borderPane.setCenter(achor1);

    }

    @FXML
    public void action2() {
        //anchor2 

        borderPane.setCenter(achor2);
    }

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

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