您好,我有以下代码,我在 anchor Pane 内添加一个圆圈,并在圆圈上添加文本。
Circle是一个Circle,ArrayList,有多个圆圈。
pane.getChildren().addAll(circle);
pane.getChildren().addAll(circle.stream().map(circ -> {
Label text = new Label(circ.getId());
text.setAlignment(Pos.CENTER);
AnchorPane.setLeftAnchor(text, circ.getCenterX());
AnchorPane.setTopAnchor(text, circ.getCenterY());
return text;
}).toArray(Label[]::new));
}
我可以使用circle.get(i).setCenterY();更改我的圈子的位置
但是如何更改添加到圆圈上的文本的位置?
提前谢谢
最佳答案
要访问标签,您需要保留对它们的引用,例如
pane.getChildren().addAll(circle);
List<Label> labels = circle.stream().map(circ -> {
Label text = new Label(circ.getId());
text.setAlignment(Pos.CENTER);
AnchorPane.setLeftAnchor(text, circ.getCenterX());
AnchorPane.setTopAnchor(text, circ.getCenterY());
return text;
}).collect(Collectors.toList());
pane.getChildren().addAll(labels);
现在你当然可以这样做
AnchorPane.setLeftAnchor(labels.get(i), newXValue);
AnchorPane.setTopAnchor(labels.get(i), newYValue);
关于java - 更改 AnchorPane 内文本的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36061715/