首页 > 解决方案 > 如何使用 JavaFX CSS 删除带有嵌套标签的按钮的填充

问题描述

如您所见,标签(“a123”)嵌套在按钮中。由于标签有背景颜色,我希望它能够吞没按钮的整个高度和左侧。你能建议正确的方法吗?

现在的情况:

在此处输入图像描述

期望:

在此处输入图像描述

我的解决方案:

<Button fx:id="rootContainer" alignment="TOP_LEFT" graphicTextGap="10.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="30.0" prefWidth="190.0" stylesheets="@../../../commons/util/resources/myCSS.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.features.dashboard.userorder.UserOrderPresenter">
    <graphic>
        <HBox alignment="CENTER" spacing="10.0">
            <children>
                  <Label fx:id="labelTableName" alignment="CENTER_RIGHT" contentDisplay="CENTER" focusTraversable="false" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" prefWidth="70.0" styleClass="label-table-number" text="a123" textFill="WHITE">
                      <HBox.margin>
                          <Insets bottom="-8.0" left="-15.0" top="-8.0" />
                      </HBox.margin>
                  </Label>
                <HBox alignment="CENTER_LEFT" spacing="5.0" HBox.hgrow="ALWAYS" />
            </children>
        </HBox>
    </graphic>
</Button>

标签: javacssjavafx

解决方案


您可以尝试负边距。

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="77.0">
         <graphic>
            <HBox>
               <children>
                  <Label maxHeight="1.7976931348623157E308" style="-fx-background-color: black; -fx-text-fill: white;" text="Label">
                     <HBox.margin>
                        <Insets bottom="-3.0" left="-7.0" right="-3.0" top="-3.0" />
                     </HBox.margin>
                     <padding>
                        <Insets left="5.0" right="5.0" />
                     </padding>
                  </Label>
               </children>
            </HBox>
         </graphic>
      </Button>
   </children>
</StackPane>

在此处输入图像描述


推荐阅读