我在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>
Run Code Online (Sandbox Code Playgroud)
如下所示.按钮周围有相当多的间距,我想将其减少到0或者可能是单个像素.我没有看到任何可以执行此操作的AnchorPane或TitledPane的属性.有这样的财产吗?

Jam*_*s_D 13
使用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>
Run Code Online (Sandbox Code Playgroud)
或者带有外部样式表
.titled-pane > * > * > AnchorPane {
-fx-padding: 0em ;
}
Run Code Online (Sandbox Code Playgroud)
我已经回答了一个类似的问题,但是我也想在这里发布它,因为它也与该问题完全匹配。
扩展@James_D的答案:
如果您不用AnchorPane作TitledPane的内容,而是其他节点,则必须相应地调整CSS。
以下是一种通用的CSS解决方案:TitledPane无论使用哪种类型的Node内容,都可以删除。内容上的填充:
.titled-pane > .content > * {
-fx-padding: 0px;
}
Run Code Online (Sandbox Code Playgroud)
会将内容的任何子项的填充设置TitledPane为0px。