更改AnchorPane中文本的位置?

时间:2016-03-17 13:17:59

标签: java javafx

您好我有以下代码,我在我的锚板内添加一个圆圈,并在圆圈上添加文字。

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();

更改圆圈中的位置

但是,如何更改我添加到圆圈中的文字的位置?

提前谢谢

1 个答案:

答案 0 :(得分:1)

要访问标签,您需要保留对它们的引用,例如

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);