如何在javafx中获取label.getWidth() [英] How to get label.getWidth() in javafx

查看:818
本文介绍了如何在javafx中获取label.getWidth()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

                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

这样做:

                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?

推荐答案

要获得调用prefWidth(-1)和prefHeight(-1)所需的宽度,布局边界仅在控件为通过resizeRelocate进行布局

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

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

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

 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));

这篇关于如何在javafx中获取label.getWidth()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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