JavaFX:我如何才能最好地将Label放在一个Shape中心? [英] JavaFX: How can I best place a Label centered in a Shape?

查看:544
本文介绍了JavaFX:我如何才能最好地将Label放在一个Shape中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的屏幕上已经有了一个Shape。例如:

 圆圈=新圆圈(x,y,半径); 
circle.setFill(Color.YELLOW);
root.getChildren()。add(circle);

我想在Circle上创建一个标签,使标签在Circle中居,字体大小最大化以适应Circle内等。



我可以看到如何通过绑定实现这一点,但如果位置/大小,这似乎是不必要的复杂这些东西在运行期间永远不会改变。



提前感谢您的帮助!我是JavaFX的新手,并不是最初在编程方面经验丰富的所有人,所以如果我能通过我的研究找到这个,我很抱歉。

解决方案

使用


相关问题的答案讨论了文本的不同边界类型(例如 Visual bounds),万一你需要。


Let's say I already have a Shape on the screen. For example:

Circle circle = new Circle(x, y, radius);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);

I would like to create a Label "over" that Circle such that the Label is centered in the Circle, the font size is maximized to fit inside the Circle, etc.

I can see how this could be accomplished via binding, but that seems needlessly complicated if the position/size of these things will never change during runtime.

Thank you in advance for your help! I'm very new to JavaFX and not all that experienced at programming in the first place, so I apologize if I should've been able to find this out via my research.

解决方案

Use a StackPane to automatically center the text on top of the shape.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.*;
import javafx.stage.Stage;

// java 8 code.
public class Circular extends Application {
    public static void main(String[] args) throws Exception {
        launch(args);
    }

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

        StackPane layout = new StackPane();
        layout.getChildren().addAll(
                circle,
                text
        );
        layout.setPadding(new Insets(20));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private Text createText(String string) {
        Text text = new Text(string);
        text.setBoundsType(TextBoundsType.VISUAL);
        text.setStyle(
                "-fx-font-family: \"Times New Roman\";" +
                "-fx-font-style: italic;" +
                "-fx-font-size: 48px;"
        );

        return text;
    }

    private Circle encircle(Text text) {
        Circle circle = new Circle();
        circle.setFill(Color.ORCHID);
        final double PADDING = 10;
        circle.setRadius(getWidth(text) / 2 + PADDING);

        return circle;
    }

    private double getWidth(Text text) {
        new Scene(new Group(text));
        text.applyCss();

        return text.getLayoutBounds().getWidth();
    }
}

Related

The answer to the related question discusses different bounds types for text (such as Visual bounds), in case you need that.

这篇关于JavaFX:我如何才能最好地将Label放在一个Shape中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆