替换 JavaFX GridPane 中 (row,col) 处的节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27154996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace a node at (row,col) in a JavaFX GridPane
提问by JabbaWook
I am making a grid-style game/simulation based on bugs "sensing" and eating food. I am using a gridPane (called worldGrid
) of labels to show the grid of bugs and food. This is obviously going to be constantly updated when a bug moves cells towards food etc.
我正在制作一个基于错误“感知”和吃食物的网格风格的游戏/模拟。我正在使用worldGrid
标签的 gridPane(称为)来显示虫子和食物的网格。当虫子将细胞移向食物等时,这显然会不断更新。
I currently have a function updateGrid(int col, int row, String cellContent)
which I want to replace the label at [row,col] with a label that has the new text in cellContent.
我目前有一个函数updateGrid(int col, int row, String cellContent)
,我想将 [row,col] 处的标签替换为在 cellContent 中有新文本的标签。
I have the follow which works
我有以下工作
worldGrid.add(new Label(cellContent), row,col);
however im worried that that is just adding a label on top of the current label and obviously over 100 iterations of the simulation thats not ideal.
但是我担心这只是在当前标签上添加一个标签,显然超过 100 次的模拟迭代并不理想。
I have tried this before adding the label:
我在添加标签之前试过这个:
worldGrid.getChildren().remove(row,col);
However I then get an IllegalArgumentException
when trying to do the add
line.
但是,IllegalArgumentException
当我尝试做这add
条线时,我得到了一个。
Any ideas on how to do this? Or even better, any ideas on how best to show a constantly changing grid that will eventually use sprites instead of text?
关于如何做到这一点的任何想法?或者甚至更好,关于如何最好地显示最终将使用精灵而不是文本的不断变化的网格的任何想法?
采纳答案by Jens-Peter Haack
The col/row provided by grid.add(node, col, row) (ATTENTION first comes col!) is only a layout constraint. This does not provide any means to access columns or rows like slots in a 2-dimensional array. So to replace a node, you have to know its object itself, e.g. remember them in a separate array.
grid.add(node, col, row) 提供的 col/row (注意首先是 col!)只是一个布局约束。这不提供任何方法来访问列或行,如二维数组中的插槽。所以要替换一个节点,你必须知道它的对象本身,例如在一个单独的数组中记住它们。
Then you are able to call getChildren().remove(object)... e.g.:
然后你就可以调用 getChildren().remove(object)... 例如:
GridPane grid = new GridPane();
Label first = new Label("first");
Label second = new Label("second");
grid.add(first, 1, 1);
grid.add(second, 2, 2);
second.setOnMouseClicked(e -> {
grid.getChildren().remove(second);
grid.add(new Label("last"), 2, 2);
});
box.getChildren().addAll(grid);
回答by T-and-M Mike
I agree with Jens-Peter but I would add that you can use GridPane's getColumnIndex and getRowIndex to obtain a particular node's location.
我同意 Jens-Peter 的观点,但我想补充一点,您可以使用 GridPane 的 getColumnIndex 和 getRowIndex 来获取特定节点的位置。
For example ( this is in a component which extends GridPane ):
例如(这是在扩展 GridPane 的组件中):
// set the appropriate label
for (Node node : getChildren()) {
if (node instanceof Label
&& getColumnIndex(node) == column
&& getRowIndex(node) == row) {
((Label)node).setTooltip(new Tooltip(tooltip));
}
}
in other words, go through all the nodes in the GridPane and check for a match of the conditions you want, in this case row and column.
换句话说,遍历 GridPane 中的所有节点并检查您想要的条件是否匹配,在本例中为行和列。
回答by PhucLy
You called getChildren().remove() method directly will cause the gridpane to go out of sync with the constraints. When you add, it also setup the constraint for the node. Add clearConstraints() method.
您直接调用 getChildren().remove() 方法将导致网格窗格与约束不同步。添加时,它还为节点设置了约束。添加 clearConstraints() 方法。