【问题标题】:How to set/remove insets in JavaFX TitledPane如何在 JavaFX TitledPane 中设置/删除插图
【发布时间】:2014-04-14 16:18:56
【问题描述】:

我在 JavaFX 中创建了一个带有单个子组件(按钮)的 TitledPane,如下所示:

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <TitledPane animated="false" layoutX="137.0" layoutY="60.0" prefHeight="400.0" prefWidth="600.0" text="untitled" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" >
                    <children >
                        <Button layoutX="193.1875" layoutY="133.5" mnemonicParsing="false" prefHeight="374.0" prefWidth="598.0" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                    </children>
                </AnchorPane>
            </content>
        </TitledPane>
    </children>
</AnchorPane>

如下所示。按钮周围有相当多的间距,我想将其减少到 0 或者可能是单个像素。我看不到 AnchorPane 或 TitledPane 的任何属性会执行此操作。有这样的属性吗?

【问题讨论】:

    标签: javafx javafx-8


    【解决方案1】:

    使用Scenic View 解决此类问题。

    TitledPane 内的 AnchorPane 在所有边都有 0.8em (10px) 的填充。这是在默认样式表 modena.css 中定义的。您可以在 FXML 中重置它:

    <?import java.lang.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.geometry.Insets?>
    
    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
        <children>
            <TitledPane animated="false" text="untitled" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <content>
                    <AnchorPane >
                    <padding>
                            <Insets top="0" right="0" left="0" bottom="0"/>                 
                    </padding>
                        <children >
                            <Button mnemonicParsing="false" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                        </children>
                    </AnchorPane>
                </content>
            </TitledPane>
        </children>
    </AnchorPane>
    

    或使用外部样式表

    .titled-pane > * > * > AnchorPane {
        -fx-padding: 0em ;
    }
    

    【讨论】:

    • 关于风景的好建议
    • @James_D:我已经尝试过了,但由于某种原因,CSS 解决方案不起作用。我为此提出了一个新问题:stackoverflow.com/questions/29607725/…
    • 这行为很奇怪。 0 已经是默认填充值,但是,您需要将 0 填充显式放入 FXML。在 SceneBuilder 中指定 0 padding 不会将它们放入 FXML,然后就会有 padding。
    【解决方案2】:

    我已经answered on a similar question,但我也想在这里发布,因为它也完全符合这个问题。

    扩展@James_D 的答案:

    如果你不使用AnchorPane作为TitledPane的内容,而是使用其他一些节点,你必须相应地调整CSS。

    用于删除TitledPane 内容上的填充的通用 CSS 解决方案(无论使用哪种类型的 Node 作为内容)如下:

    .titled-pane > .content > * {
        -fx-padding: 0px;
    }
    

    这会将TitledPane 内容的任何子项的填充设置为0px

    【讨论】:

      猜你喜欢
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多