我在我的一个项目中一直在使用javafx作为GUI。它应该具有添加条目,删除条目,查看条目,修改条目和搜索条目的功能。我已经完成了所有这些工作,并且一直在努力使GUI变得更清洁,将顶部的导航栏放在顶部,同时让GUI的其余部分随着具体任务的变化而变化。但是,它一直在显示一些我无法修复的奇怪行为(最好)。Javafx NavBar问题
这是代码的相关部分:
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'正在'试图改变它的内部',但我不确定这一点。我很感激有人告诉我为什么这不按我的意图工作,以及有关如何解决此问题的建议。
谢谢你,这个完美的作品。 – narf0708