Javafx 导航栏问题

Javafx NavBar Issue

我一直在我的一个项目中使用 javafx 作为 GUI。它应该具有添加条目、删除条目、查看条目、修改条目和搜索条目的功能。我已经完成了所有这些事情,并且一直在努力使 GUI 更简洁,方法是在顶部放置一个导航栏,同时让 GUI 的其余部分根据手头的特定任务进行更改。但是,它一直显示出一些我无法修复的奇怪行为(充其量)。

这是代码的相关部分:

private static Scene makeScene(Stage primaryStage, BorderPane searchRoot, BorderPane addRoot, BorderPane viewRoot){
    final Scene[] scene = {null};
    final Scene searchScene;
    final Scene addScene;
    final Scene viewScene;

    final int WIDTH = 500;
    final int HEIGHT = 600;

    HBox navBar = new HBox(15);
    Button searchButton = new Button("Search People");
    Button addButton    = new Button("Add Person");
    Button viewButton   = new Button("View People");

    navBar.getChildren().addAll(searchButton, addButton, viewButton);
    navBar.setAlignment(Pos.CENTER);
    navBar.setPrefHeight(42);
    searchRoot.setTop(navBar);
    addRoot.setTop(navBar);
    viewRoot.setTop(navBar);

    searchScene = new Scene(searchRoot, WIDTH, HEIGHT);
    addScene    = new Scene(addRoot,    WIDTH, HEIGHT);
    viewScene   = new Scene(viewRoot,   WIDTH, HEIGHT);
    scene[0] = viewScene;

    searchButton.setOnAction((ActionEvent event) -> {
        System.out.println("Search Button Pressed");
        scene[0] = searchScene;
        primaryStage.setScene(scene[0]);
    });
    addButton.setOnAction((ActionEvent event) -> {
        System.out.println("Add Button Pressed");
        scene[0] = addScene;
        primaryStage.setScene(scene[0]);
    });
    viewButton.setOnAction((ActionEvent event) -> {
        System.out.println("Soup");
        scene[0] = viewScene;
        primaryStage.setScene(scene[0]);
    });

    return scene[0];
}

以这种方式调用:

    primaryStage.setScene(makeScene(primaryStage, searchRoot, addRoot, viewRoot));
    primaryStage.show();

当按下导航栏上的任何按钮时,它确实会切换到适当的面板,但导航栏本身会消失,从而无法进行进一步的导航。关于为什么会发生这种情况,我最好的猜测是因为 navBar 'lives' 在它试图更改的内容中,但我不确定。如果有人能告诉我为什么这不像我预期的那样工作,以及如何解决这个问题的建议,我将不胜感激。

您的猜测或多或少是正确的。 primaryStage.setScene(...) 将替换舞台的全部内容。此外,一个节点只能有一个父节点,因此当您将 navBar 设置为三个不同的 BorderPane 的顶部时,行为未定义。 (我 认为 它只会出现在最后一个 viewRoot 中,但这完全取决于实现。)

我建议您重构它:使用 BorderPane 作为(唯一的)Scene 的根。将导航栏放在边框窗格的顶部,并通过在边框窗格上调用 setCenter(...) 来更改内容,传入 searchRootaddRootviewRoot 作为合适。

类似于:

private static void makeScene(Stage primaryStage, Node searchRoot, Node addRoot, Node viewRoot){

    final int WIDTH = 500;
    final int HEIGHT = 600;

    BorderPane root = new BorderPane();

    final Scene scene = new Scene(root, WIDTH, HEIGHT);


    HBox navBar = new HBox(15);
    Button searchButton = new Button("Search People");
    Button addButton    = new Button("Add Person");
    Button viewButton   = new Button("View People");

    navBar.getChildren().addAll(searchButton, addButton, viewButton);
    navBar.setAlignment(Pos.CENTER);
    navBar.setPrefHeight(42);

    root.setTop(navBar);

    searchButton.setOnAction((ActionEvent event) -> {
        System.out.println("Search Button Pressed");
        root.setCenter(searchRoot);
    });
    addButton.setOnAction((ActionEvent event) -> {
        System.out.println("Add Button Pressed");
        root.setCenter(addRoot);
    });
    viewButton.setOnAction((ActionEvent event) -> {
        System.out.println("Soup");
        root.setCenter(viewRoot);
    });

    primaryStage.setScene(scene);
}