How to get label.getWidth() in javafx

前端 未结 4 925
余生分开走
余生分开走 2021-01-03 04:11

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?

                       


        
4条回答
  •  甜味超标
    2021-01-03 04:21

    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

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

提交回复
热议问题