2012-06-21 21 views
5

我想在JavaFX中製作一個簡單的遊戲(這是一個學校工作),並且我試圖用板清理面板,然後重新繪製它。我已經嘗試了很多方法,而且這是我發現的唯一一個可以刪除所有棋盤(可視化)的部分,而不會創建顯示已經被刪除但仍然顯示的棋子的視覺錯誤。在JavaFX中清理gridPane並維護Grid線

所以我聲明gridPane這樣的:

private GridPane gridPecas; 

@Override 
public void start(Stage primaryStage) 
{ 
    gridPecas = new GridPane(); 
    gridPecas.setGridLinesVisible(true); 

    paintBoard(); 

    // rest of the code to create and paint the Stage 
} 

private void paintBoard() 
{ 
    gridPecas.getChildren().clear(); 
    // Code to fill the board with ImageView and Images of the pieces 
} 

這種方法的問題是,當 「gridPecas.getChildren()清除();」被稱爲我只是從鬆散網格線。

我該如何解決這個問題?

回答

0

嗯我的建議是不清除所有的孩子。總是離開imageViews,只是替換他們顯示的圖像。爲了「清楚」董事會提出了一個「空」的形象。

+0

感謝您的回答,但我剛剛找到了完美的解決方案xD而不是「gridPecas.getChildren()。clear();」我只是在for循環中這樣做:「gridPecas.getChildren()。removeAll(pecasFX [i] [j]);」因此,當閱讀這篇文章時,它會從網格中刪除它,並且,由於原始的「填充板」代碼,他有一個新的邊界清理和更新。 – aliasbody

3

清除GridPane和維護網格線是爲後續的方式:

Node node = grid.getChildren().get(0); 
    grid.getChildren().clear(); 
    grid.getChildren().add(0,node); 

GridPane中的第一個Node *(*如果在添加網格中的元素之前設置GridLinesVisible)包含網格線(也許o其他事情)。

所以你只需要明確後

1

&重新添加一個一個班輪之前保持這個節點:。grid.getChildren()中的retainAll(grid.getChildren()得到(0) );