Remove padding/margin from JavaFX Label

前端 未结 4 1785
长情又很酷
长情又很酷 2020-12-11 03:44

Is there a way to remove the default space (padding/margin) that JavaFX label adds? I want to get rid of the space displayed between the black lines on the image below:

4条回答
  •  余生分开走
    2020-12-11 04:15

    You can achieve that by adding -fx-padding: -10 0 0 0; to the list of your styles.

    For more flexible solution you can use FontMetrics information:

    FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
    label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));
    

    NB: You need to call that code after scene.show(). Before that graphics engine is not ready to provide correct metrics.

提交回复
热议问题