2014-04-14 42 views
6

我创建了一个JavaFX的一个TitledPane具有如下单个子组件(按钮)插图:如何设置/删除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会执行此操作的任何属性。有这样的财产吗?

enter image description here

回答

11

使用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 ; 
} 
+0

风景优美的建议 – PhilDin

+0

@James_D:我试过这个,但由于某种原因CSS解决方案无法正常工作。我为此提出了一个新问题:http://stackoverflow.com/questions/29607725/remove-insets-in-javafx-titledpane-with-css-not-working – Perneel

1

我已经answered on a similar question,但我也希望它张贴在这里,因为它也完美这个问题相匹配。

为了延长@ James_D的回答是:

如果不使用AnchorPaneTitledPane的内容,但一些其他的节点上,你必须调整相应的CSS。

用于在TitledPane的内容去除填充,不管哪种类型的Node被用作内容的通用CSS溶液,如下:

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

这将设置衬垫的TitledPane的任何子的内容为0px