gpt4 book ai didi

java - 文本未出现在 JavaFX 中

转载 作者:行者123 更新时间:2023-12-01 08:59:39 25 4
gpt4 key购买 nike

我正在尝试显示我正在制作的游戏的得分,但由于某种原因,一旦添加到组中,文本就不会显示。

这是我的代码:

Text scoreText = new Text(scoreString);

scoreText.setFont(new Font("ARIAL", 30);
scoreText.setStyle("-fx-font-weight: bold;");
scoreText.setFill(Color.WHITE);

Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scoreText);

最佳答案

您正在尝试将 scoreText 添加到两个不同的父项:此处:

Pane scorePane = new Pane(scoreText);

这使得 scoreText 成为 scorePane 的子级,这里再一次:

root.getChildren().add(scoreText);

这使得 scoreText 成为 root 的子级。由于节点不能在场景图中出现两次,因此这是行不通的。

如果您希望将 scoreText 封装在 Pane 中,请将其添加到 Pane 中并将 Pane 添加到 root 中:

Text scoreText = new Text(scoreString);

// ...

Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scorePane);

如果您在 Pane 中不需要它,则只需将其直接添加到root:

Text scoreText = new Text(scoreString);

// ...

// omit this:
// Pane scorePane = new Pane(scoreText);

scoreText.relocate(100, 100);

root.getChildren().add(scoreText);

关于java - 文本未出现在 JavaFX 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com