java - 更改 AnchorPane 内文本的位置?

标签 java javafx

您好,我有以下代码,我在 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/

相关文章:

java - Spring Autowiring 依赖,具有构造函数参数

java - 函数返回时成员变量为 null

java - 运行 javafx 程序时出现 InvocationTargetException

JavaFX 客户端 UI 不会显示主机触发的警报

java - 为什么不显式调用 finalize() 或启动垃圾收集器?

java - 使用java的多列顺序

java - 如何正确使用mediaPlayer?

java - Oracles JavaFX 示例中鼠标单击位置标签错误?

java - 如何用新列表替换ListView的列表

java - 如何使用默认构造函数从另一个类启动 javafx 应用程序?