javafx-2 - 如何将文本放入圆形对象中以从圆形的中心显示它?

标签 javafx-2 javafx

我很好奇有什么方法可以将文本(我通常会使用会动态变化的数字)放入圆形对象或创建文本对象并将其边界设置为圆形的中心是显示它的唯一方法?我会感谢您的每一个回复:)

最佳答案

将圆和文本放在 StackPane 中,并将文本边界类型计算设置为 VISUAL:

Circle circle = new Circle();
Text text = new Text("42");
text.setBoundsType(TextBoundsType.VISUAL); 
StackPane stack = new StackPane();
stack.getChildren().add(circle, text);

您可以使用时间线来更改圆圈中文本的值。

这是一个完整的例子:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TextInCircle extends Application {
    public static void main(String[] args) throws Exception { launch(args); }

    private static final int R = 150;
    private static final Color lineColor = Color.FIREBRICK.deriveColor(0, 1, 1, .6);

    @Override
    public void start(final Stage stage) throws Exception {
        final Circle circle = createCircle();
        final Text   text   = createText();

        final Line l1 = createLine(lineColor, 0, R - 0.5, 2 * R, R - 0.5);
        final Line l2 = createLine(lineColor, R - 0.5, 0, R - 0.5, 2 * R);

//        Group group = new Group(circle, text, l1 , l2);
        Group group = new Group(circle, l1 , l2);

        StackPane stack = new StackPane();
        stack.getChildren().addAll(group, text);

        stage.setScene(new Scene(stack));
        stage.show();

        animateText(text);
    }

    private Circle createCircle() {
        final Circle circle = new Circle(R);

        circle.setStroke(Color.FORESTGREEN);
        circle.setStrokeWidth(10);
        circle.setStrokeType(StrokeType.INSIDE);
        circle.setFill(Color.AZURE);
        circle.relocate(0, 0);

        return circle;
    }

    private Line createLine(Color lineColor, double x1, double y1, double x2, double y2) {
        Line l1 = new Line(x1, y1, x2, y2);

        l1.setStroke(lineColor);
        l1.setStrokeWidth(1);

        return l1;
    }

    private Text createText() {
        final Text text = new Text("A");

        text.setFont(new Font(30));
        text.setBoundsType(TextBoundsType.VISUAL);
//        centerText(text);

        return text;
    }

    private void centerText(Text text) {
        double W = text.getBoundsInLocal().getWidth();
        double H = text.getBoundsInLocal().getHeight();
        text.relocate(R - W / 2, R - H / 2);
    }

    private void animateText(final Text text) {
        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent actionEvent) {
                char newValue = (char) ((text.getText().toCharArray()[0] + 1) % 123);
                if (newValue == 0) newValue = 'A';
                text.setText("" + newValue);
//                centerText(text);
            }
        }));
        timeline.setCycleCount(1000);
        timeline.play();
    }
}

centeredtext

注释掉的代码还包括用于手动放置文本的代码,如果您更喜欢这样做而不是使用 StackPane。

关于javafx-2 - 如何将文本放入圆形对象中以从圆形的中心显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17437411/

相关文章:

css - JavaFX 从资源文件夹加载 CSS 样式

java - FX - 在网格中对齐复选框的建议

javafx-2 - 我如何在 Javafx2 中使用 jfreeChart

javafx 使用自动完成功能与数据库

javafx - JSoup 随机抛出 java.io.IOException : stream is closed when running from browser

java - 初级阶段改变场景大小

java - 将 ListView 绑定(bind)到 ObservableSet

listview - JavaFX 2 中的奇怪控件宽度?

javafx-2 - 在 JavaFX 的同一个场景中多次重用同一个 ImageView

java - 如何在 JavaFX 2.0 中创建几何形状?