Javafx NavBar问题

时间:2015-05-11 00:35:15

标签: java javafx navigation

我在其中一个项目中一直使用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();

当按下导航栏上的任何按钮时,它会切换到正确的面板,但导航栏本身会消失,从而无法进一步导航。我最好猜测为什么会发生这种情况是因为导航栏生活了#39;它试图改变的内部,但我不确定。我很感激有人告诉我为什么这不符合我的意图,以及如何解决这个问题的建议。

1 个答案:

答案 0 :(得分:0)

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

我建议你重构一下:使用Scene作为(唯一的)setCenter(...)的根。将导航栏放在边框窗格的顶部,并通过调用边框窗格上的searchRoot更改内容,并传入addRootviewRootprivate 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); } 作为合适的。

类似的东西:

inputEditText