从JavaFX Label中删除填充/边距 [英] Remove padding/margin from JavaFX Label
本文介绍了从JavaFX Label中删除填充/边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法删除JavaFX标签添加的默认空间(填充/边距)?我想摆脱下图中黑线之间显示的空间:
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:
源代码:
public class LabelTest extends Application
{
@Override
public void start(final Stage primaryStage)
{
final Group root = new Group();
final Scene scene = new Scene(root, 300, 130, Color.WHITE);
final GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);
final Label label = new Label("Label");
label.setStyle("-fx-font-size:44px;-fx-font-weight: bold;-fx-text-fill:#5E34B1;-fx-background-color:#ffc300;");
GridPane.setHalignment(label, HPos.CENTER);
gridpane.add(label, 0, 0);
root.getChildren().add(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
推荐答案
你可以通过将 -fx-padding:-10 0 0 0;
添加到样式列表中来实现。
You can achieve that by adding -fx-padding: -10 0 0 0;
to the list of your styles.
对于更灵活的解决方案,您可以使用 FontMetrics
信息:
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:您需要在 scene.show()
。之前图形引擎尚未准备好提供正确的指标。
NB: You need to call that code after scene.show()
. Before that graphics engine is not ready to provide correct metrics.
这篇关于从JavaFX Label中删除填充/边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文