有没有办法删除 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();
}
}
最佳答案
执行此操作的一种更动态的方法是使用 Text
而不是 Label 并将 boundsType
设置为 VISUAL
。这会导致文本的任何一侧都没有任何填充,无论字体大小如何。
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
关于java - 从 JavaFX 标签中删除填充/边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359852/