2014-04-14 38 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