Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JavaFX标签中删除填充/边距_Java_Css_Javafx_Javafx 2_Javafx 8 - Fatal编程技术网

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

从JavaFX标签中删除填充/边距,java,css,javafx,javafx-2,javafx-8,Java,Css,Javafx,Javafx 2,Javafx 8,有没有办法删除JavaFX标签添加的默认空格(填充/边距)?我想去掉下图中黑线之间显示的空间: 源代码: public class LabelTest extends Application { @Override public void start(final Stage primaryStage) { final Group root = new Group(); final Scene scene = new Scene(root

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

源代码:

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添加到样式列表中

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

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

NB:您需要在
scene.show()之后调用该代码。在此之前,图形引擎还没有准备好提供正确的度量。

执行此操作的一个更动态的方法是使用
文本
而不是标签,并将
边界类型设置为
可视
。这样,无论字体大小如何,文本的任何一侧都不会有任何填充

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

有关更多详细信息,请参阅我的帖子

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

label.setPadding(new Insets(-2,0,0,0)); //top, right, bottom, left
以分隔符为例:

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

stage.show()

或者通过这种方式索引0,因为它在索引0处只有一个元素,这是一个样式为cals的区域。line

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

对我来说,使用设置填充是最简单的

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

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

除了硬编码值,还有什么动态方法吗?我不想硬编码一个值(在你的例子中为-10),然后看到实际的文本被切碎。如果有其他控件,我可以使用它,默认情况下不添加填充,只添加文本?我们不能使用CSS设置它吗?