【问题标题】:Javafx NavBar IssueJavafx 导航栏问题
【发布时间】:2015-05-11 00:35:14
【问题描述】:

我一直在我的一个项目中将 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 上的任何按钮时,它确实会切换到正确的面板,但 navBar 本身会消失,从而无法进行进一步的导航。关于为什么会发生这种情况,我最好的猜测是因为导航栏“生活”在它试图改变的东西中,但我不确定这一点。如果有人能告诉我为什么这不能按我的预期工作,以及如何解决这个问题的建议,我将不胜感激。

【问题讨论】:

    标签: java javafx navigation


    【解决方案1】:

    你的猜测或多或少是正确的。 primaryStage.setScene(...) 将替换整个舞台内容。此外,一个节点只能有一个父节点,因此当您将navBar 设置为三个不同BorderPanes 的顶部时,行为是不确定的。 (我认为它只会出现在最后一个,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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 2020-08-17
      • 2016-04-24
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多