java 从 JavaFX 标签中删除填充/边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34359852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:48:44  来源:igfitidea点击:

Remove padding/margin from JavaFX Label

javacssjavafxjavafx-2javafx-8

提问by SDS

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:

有没有办法删除 JavaFX 标签添加的默认空间(填充/边距)?我想去掉下图黑线之间显示的空间:

enter image description here

在此处输入图片说明

Source code:

源代码:

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

回答by Sergey Grinev

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

您可以通过添加-fx-padding: -10 0 0 0;到您的样式列表来实现这一点。

For more flexible solution you can use FontMetricsinformation:

要获得更灵活的解决方案,您可以使用以下FontMetrics信息:

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.

注意:您需要在scene.show(). 在此之前,图形引擎还没有准备好提供正确的指标。

回答by ItachiUchiha

One of the more dynamic ways to do this is to use a Textinstead of a Label and set the boundsTypeas VISUAL. This results in a Text without any padding on the any of the sides of the Text, irrespective of the font size.

执行此操作的一种更动态的方法是使用 aText而不是 Label 并将 设置boundsTypeVISUAL。这会导致文本在文本的任何一侧都没有任何填充,而不管字体大小。

Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);

回答by Anja

For me it was easiest to just use setPadding.

对我来说,最简单的方法是使用setPadding

label.setPadding(new Insets(-2,0,0,0)); //top, right, bottom, left

This way I did not have to deal with the css-style sheet.

这样我就不必处理 css 样式表。

回答by Mosch

For more details see my post here Substructure styling

有关更多详细信息,请参阅我在此处的帖子子结构样式

You could also do it like this after the stage.show().

你也可以在stage.show()之后这样做。

With the example of an separator:

以分隔符为例:

Separator separator = new Separator();
separator.setStyle(""
    + "-fx-border-width: 1px;"
    + "-fx-border-color: black;"
    + "-fx-padding: 0px;"
    + "");

stage.show()

Do

Node line = separator.lookup(".line");
line.setStyle(""
    + "-fx-border-insets: 0px;"
    + "-fx-border-width: 0px;"
    + "");

or this way index 0because it has only one element at index 0 which is an Region with style calss .line

或者这样索引0,因为它在索引 0 处只有一个元素,它是一个带有样式 calss .line 的区域

separator.getChildrenUnmodifiable().get(0).setStyle(""
    + "-fx-border-insets: 0px;"
    + "-fx-border-width: 0px;"
    + "");