从 JavaFX 标签中删除 padding/margin
Remove padding/margin from JavaFX Label
有没有办法删除 JavaFX 标签添加的默认 space (padding/margin)?我想去掉下图中黑线之间显示的 space:
源代码:
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;
添加到样式列表来实现。
要获得更灵活的解决方案,您可以使用 FontMetrics
信息:
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));
注意: 您需要在 scene.show()
之后调用该代码。在此之前,图形引擎还没有准备好提供正确的指标。
执行此操作的一种更动态的方法是使用 Text
而不是标签并将 boundsType
设置为 VISUAL
。这会导致文本的任何一侧都没有任何填充,无论字体大小如何。
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
有关详细信息,请参阅我的 post 此处
你也可以在 stage.show().
之后这样做
以分隔符为例:
Separator separator = new Separator();
separator.setStyle(""
+ "-fx-border-width: 1px;"
+ "-fx-border-color: black;"
+ "-fx-padding: 0px;"
+ "");
stage.show()
做
Node line = separator.lookup(".line");
line.setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
或者这样索引 0 因为它在索引 0 处只有一个元素,它是一个带有样式 calss .line[= 的区域14=]
separator.getChildrenUnmodifiable().get(0).setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
对我来说,最简单的方法就是使用 setPadding。
label.setPadding(new Insets(-2,0,0,0)); //top, right, bottom, left
这样我就不用去处理 css-style sheet.
有没有办法删除 JavaFX 标签添加的默认 space (padding/margin)?我想去掉下图中黑线之间显示的 space:
源代码:
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;
添加到样式列表来实现。
要获得更灵活的解决方案,您可以使用 FontMetrics
信息:
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));
注意: 您需要在 scene.show()
之后调用该代码。在此之前,图形引擎还没有准备好提供正确的指标。
执行此操作的一种更动态的方法是使用 Text
而不是标签并将 boundsType
设置为 VISUAL
。这会导致文本的任何一侧都没有任何填充,无论字体大小如何。
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
有关详细信息,请参阅我的 post 此处
你也可以在 stage.show().
之后这样做以分隔符为例:
Separator separator = new Separator();
separator.setStyle(""
+ "-fx-border-width: 1px;"
+ "-fx-border-color: black;"
+ "-fx-padding: 0px;"
+ "");
stage.show()
做
Node line = separator.lookup(".line");
line.setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
或者这样索引 0 因为它在索引 0 处只有一个元素,它是一个带有样式 calss .line[= 的区域14=]
separator.getChildrenUnmodifiable().get(0).setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
对我来说,最简单的方法就是使用 setPadding。
label.setPadding(new Insets(-2,0,0,0)); //top, right, bottom, left
这样我就不用去处理 css-style sheet.