如何在javafx中获取label.getWidth()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21074024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:24:22  来源:igfitidea点击:

How to get label.getWidth() in javafx

javajavafxjavafx-2label

提问by baxbear

always when i try to get the width of an Array in Java it just returns 0 and i dont know why. Can somebody explain to me how it is done right?

总是当我尝试在 Java 中获取数组的宽度时,它只返回 0,我不知道为什么。有人可以向我解释它是如何正确完成的吗?

                Label label = new Label();
                label.setFont(Font.font(label.getFont().getFamily(), 8));
                label.setText(""
                        + a[i][j].getList().getFirst().getLength()
                        + " mal "
                        + intToColor(a[i][j].getList().getFirst()
                                .getColor()));
                label.relocate((x1 + x2) / 2 - label.getWidth() / 2, (y1 + y2) / 2);
                label.idProperty().set("trackName");
                label.setTextFill(Color.web("GREEN"));
                field.getChildren().addAll(path, label);
                System.out.println(label.getLayoutBounds().getWidth());
                System.out.println(label.getWidth());//... also i tested a lot of different getters but i couldn't get the label width (same problem with height)

Hope you got an idea what i have to do.

希望你知道我必须做什么。

@tomsontom

@tomsontom

did this:

做过这个:

                label.prefWidth(-1);
                label.prefHeight(-1);
                label.impl_processCSS(true);
//                  label.resizeRelocate(x, y, width, height);
                System.out.println(label.getWidth());

but it didnt worked - can you explain me more precisely what i need to do?

但它没有用 - 你能更准确地解释我需要做什么吗?

采纳答案by tomsontom

To get the width you need to call prefWidth(-1) and prefHeight(-1) the layout bounds are only set once the control is layouted through resizeRelocate

要获得宽度,您需要调用 prefWidth(-1) 和 prefHeight(-1) 布局边界仅在通过 resizeRelocate 布局控件后才设置

To get the correct width before the stage is shown you also need to call impl_processCSS(true) which is an NONE public API but there's nothing better at the moment IIRC

要在显示舞台之前获得正确的宽度,您还需要调用 impl_processCSS(true),这是一个 NONE 公共 API,但目前 IIRC 没有比这更好的了

 HBox h = new HBox();
 Label l = new Label("Hello");
 h.getChildren().add(l);
 Scene s = new Scene(h);
 l.impl_processCSS(true);
 System.err.println(l.prefWidth(-1)+"/"+l.prefHeight(-1));

回答by assylias

I don't think the width will be calculated until the Label is shown: add it to a Parent that is visible and you should get a non-zero result.

我认为在显示 Label 之前不会计算宽度:将其添加到可见的 Parent 中,您应该得到一个非零结果。

回答by Warren M. Nocos

Another approach is to calculate the string width of the label's textProperty using Fontloader.computeStringWidth(text, font)which would output the pixel width of the label's textProperty, and is considerably similar to getting the label's width when the label is layouted at the later part.

另一种方法是计算标签的 textProperty 的字符串宽度,使用Fontloader.computeStringWidth(text, font)它会输出标签的 textProperty的像素宽度,这与在后面部分布局标签时获取标签的宽度非常相似。

For an instance:

例如:

    FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();
    Label label = new Label("My name is Warren. I love Java.");
    label.setFont(Font.font("Consolas", FontWeight.THIN, FontPosture.REGULAR, 16));
    System.out.println("The label's width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));

The output is:

输出是:

The label's width is: 272.70312

To test:

去测试:

    FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();
    Label label = new Label("My name is Warren. I love Java.");
    label.setFont(Font.font("Consolas", FontWeight.THIN, FontPosture.REGULAR, 16));
    System.out.println("The label's textProperty string width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));
    System.out.println("Label's width before layouted: " + label.getWidth());
    primaryStage.setScene(new Scene(new StackPane(label), 300, 250));
    primaryStage.show();
    System.out.println("Label's width after layouted: " + label.getWidth());

Here's the output

这是输出

The label's textProperty string width is: 272.70312
Label's width before layouted: 0.0
Label's width after layouted: 273.0

Comparably, they are the same. Hope this helps.

相比之下,它们是相同的。希望这可以帮助。

回答by ihayet

You can try the following:

您可以尝试以下操作:

func foo()
{
    label.layout();

    Platform.runLater(()->
    {
        System.out.println(label.getWidth());
    });
}