我在我的一個項目中一直在使用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