2015-05-11 114 views
0

我在我的一個項目中一直在使用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'正在'試圖改變它的內部',但我不確定這一點。我很感激有人告訴我爲什麼這不按我的意圖工作,以及有關如何解決此問題的建議。

回答

0

你的猜測或多或少是正確的。 primaryStage.setScene(...)將取代舞臺的全部內容。另外,一個節點只能有一個父節點,因此當您將navBar設置爲三個不同BorderPane的頂部時,行爲未定義。 (我認爲它只會落得只出現在最後一節,viewRoot,但是這完全是依賴於實現的。)

我建議你重構這個:使用BorderPane作爲的根(唯一) Scene。將導航欄放在邊框窗格的頂部,並通過在邊框窗格上調用setCenter(...)來更改內容,並根據需要傳入searchRoot,addRootviewRoot

喜歡的東西:

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); 
} 
+0

謝謝你,這個完美的作品。 – narf0708