Javafx导航栏问题
我一直在我的一个项目中使用javafx作为GUI。它应该具有添加条目、删除条目、查看条目、修改条目和搜索条目的功能。我已经完成了所有这些事情,并且一直在努力使GUI更干净,方法是在顶部放置一个导航栏,同时让GUI的其余部分根据手头的特定任务进行更改。然而,它一直在显示一些我无法修复的特殊行为(充其量) 这是代码的相关部分:Javafx导航栏问题,java,javafx,navigation,Java,Javafx,Navigation,我一直在我的一个项目中使用javafx作为GUI。它应该具有添加条目、删除条目、查看条目、修改条目和搜索条目的功能。我已经完成了所有这些事情,并且一直在努力使GUI更干净,方法是在顶部放置一个导航栏,同时让GUI的其余部分根据手头的特定任务进行更改。然而,它一直在显示一些我无法修复的特殊行为(充其量) 这是代码的相关部分: private static Scene makeScene(Stage primaryStage, BorderPane searchRoot, BorderPane ad
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();
当按下导航栏上的任何按钮时,它确实会切换到正确的面板,但导航栏本身会消失,从而无法进行进一步的导航。我对为什么会发生这种情况的最好猜测是因为导航栏“生活”在它试图改变的东西的内部,但我不确定这一点。如果有人能告诉我为什么这不符合我的预期,以及如何解决这一问题的建议,我将不胜感激。你的猜测或多或少是正确的<代码>primaryStage.setScene(…)将替换该阶段的全部内容。此外,一个节点只能有一个父节点,因此当您将
navBar
设置为三个不同的BorderPane
的顶部时,行为是未定义的。(我认为它最终只会出现在最后一个视图中,viewRoot
,但这完全取决于实现。)
我建议您重构:使用边框窗格
作为(唯一的)场景
的根。将导航栏放在边框窗格的顶部,通过调用边框窗格上的setCenter(…)
来更改内容,根据需要传入searchRoot
、addRoot
或viewRoot
比如:
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);
}