在JavaFX中清理gridPane并维护网格线

ali*_*ody 7 gridlines gridpanel javafx-2

我正在尝试在JavaFX中制作一个简单的游戏(这是一项学校工作),我试图用电路板清除面板,然后重新绘制它.我已经尝试了很多方法,这是我发现的唯一一个删除了所有板块(可视化)而没有创建一个可视错误,显示已经删除但仍然显示的块.

所以我声明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
 }
Run Code Online (Sandbox Code Playgroud)

这个方法的问题在于,当"gridPecas.getChildren().clear();"时 被称为我只是松散网格线GridPanel.

我怎么解决这个问题?

Flo*_*o C 7

setGridLinesVisible(boolean) 仅用于调试:

http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html#setGridLinesVisible(boolean)

例如,您甚至无法更改 gridLines 颜色。

所以你应该找到另一种方法来制作你的网格线,然后调用

yourPane.getChildren().clear();
Run Code Online (Sandbox Code Playgroud)

因为它比 gridPecas.getChildren().removeAll(pecasFX[i][j]);


Ulu*_*Biy 0

嗯,我的建议是根本不要清除所有孩子。始终保留 imageViews 并仅替换它们显示的图像。为了“清除”黑板,放置一个“空”图像。