如何在布置标签之前获取标签的尺寸? [英] How to get the size of a label before it is laid out?

查看:93
本文介绍了如何在布置标签之前获取标签的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于如何实现此目标的旧答案.但是,由于它涉及使用impl_processCSS(boolean)(一种现已弃用的方法),我认为我们需要更新答案.

I read this old answer on how to accomplish this. But since it involves using impl_processCSS(boolean), a method that is now deprecated, I think we need to update the answer.

我尝试将标签放在HBox内,然后获取它的大小,或者获取HBox的大小,但是没有任何运气.而且我也尝试使用.label.getBoundsInLocal().getWidth().

I've tried placing the label inside a HBox and then get the size of it, or getting the size of the HBox, without any luck. And I've also tried using .label.getBoundsInLocal().getWidth().

SSCCE: 导入javafx.scene.control.Label; 导入javafx.scene.layout.HBox; 导入javafx.application.Application; 导入javafx.scene.Scene;

SSCCE: import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.application.Application; import javafx.scene.Scene;

public class SSCCE extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        HBox root = new HBox();
        Label label = new Label("foo");
        System.out.println(label.getWidth());

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

推荐答案

自JavaFX 8开始,您要查找的方法是applyCss().

Since JavaFX 8, the method you are looking for is applyCss().

如JavaDoc所述:

As JavaDoc states:

将样式应用于此节点及其子节点(如果有).通常不需要直接调用此方法,但可以将其与Parent.layout()结合使用,以在下一个脉冲之前或者如果场景不在舞台中,确定Node的大小.

apply styles to this Node and its children, if any. This method does not normally need to be invoked directly but may be used in conjunction with Parent.layout() to size a Node before the next pulse, or if the Scene is not in a Stage.

因此,您需要将节点放置在容器中,并且该节点已经在场景中,并调用layout().

So you need to have the node in the container, and this one already on the scene, and also call layout().

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Label label = new Label("foo bla bla");
    root.getChildren().add(label);
    Scene scene = new Scene(root);

    root.applyCss();
    root.layout();
    System.out.println(label.getWidth());

    primaryStage.setScene(scene);
    primaryStage.show();

}

正在输出:17.818359375

Being the output: 17.818359375

请注意,由于以下原因,我已将HBox更改为Group:

Note I've changed HBox for Group, since:

组将在布局遍历期间将其可调整大小的子控件自动调整大小"到其首选大小,以确保随着区域和控件状态的改变而正确调整其大小

a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass to ensure that Regions and Controls are sized properly as their state changes

如果使用HBox,则还需要设置场景的尺寸:

If you use an HBox, you need to set also the dimensions of the scene:

@Override
public void start(Stage primaryStage) {
    HBox root = new HBox();
    Label label = new Label("foo");
    root.getChildren().add(label);
    Scene scene = new Scene(root,100,20);

    root.applyCss();
    root.layout();
    System.out.println(label.getWidth());

    primaryStage.setScene(scene);
    primaryStage.show();

}

现在输出为:18.0

Now the output is: 18.0

这篇关于如何在布置标签之前获取标签的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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