2017-04-05 43 views
0

因爲我需要一次有多個可摺疊的TitledPanes(默認的JavaFX Accordion不支持),所以我在VBox中添加了一些TitledPanes。到目前爲止,這工作正常,但我意識到TitledPanes的寬度比實際的VBox的寬度小10px。帶有標題欄的VBox的JavaFX填充

繼FXML代碼:

<Pane prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.71" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <VBox prefHeight="700.0" prefWidth="1000.0"> 
      <children> 
       <TitledPane animated="false" text="Pane 1"> 
        <content> 
         <AnchorPane prefHeight="300.0" /> 
        </content> 
       </TitledPane> 
       <TitledPane animated="false" text="Pane 2"> 
        <content> 
         <AnchorPane prefHeight="300.0" /> 
        </content> 
       </TitledPane> 
      </children> 
     </VBox> 
    </children> 
</Pane> 

將會產生本(見右側的間隙): before

加入和適應一個CSS文件後,佈局看起來是這樣的: after

該css代碼:

VBox { 
    -fx-padding: 0 -11 0 -1; 
} 

對我來說,這個解決方案工作正常,但它看起來像一個糟糕的解決方法。我想那裏需要更聰明的解決方案?!

感謝很多提前:)

回答

1

窗格調整大小與舞臺,但垂直框的寬度不超過1000像素。 捕捉階段的寬度大約爲1010px。

你應該能夠與窗格分配:

<VBox xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <TitledPane animated="false" text="Pane 1"> 
      <content> 
       <AnchorPane prefHeight="300.0" /> 
      </content> 
     </TitledPane> 
     <TitledPane animated="false" text="Pane 2"> 
      <content> 
       <AnchorPane prefHeight="300.0" /> 
      </content> 
     </TitledPane> 
    </children> 
</VBox> 

或者使用AnchorPane調整VBOX的大小,如果由於某種原因需要上述面板:

<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
      <children> 
       <TitledPane animated="false" text="Pane 1"> 
        <content> 
         <AnchorPane prefHeight="300.0" /> 
        </content> 
       </TitledPane> 
       <TitledPane animated="false" text="Pane 2"> 
        <content> 
         <AnchorPane prefHeight="300.0" /> 
        </content> 
       </TitledPane> 
      </children> 
     </VBox> 
    </children> 
</AnchorPane> 
+0

謝謝,原來如此! ;-) – pixelstuermer

0

謝謝@geh:

調整舞臺到場景解決了問題,沒有nee一個css調整的d:

@Override 
public void start(Stage stage) throws Exception { 
    Pane root = (Pane) FXMLLoader.load(getClass().getResource("root.fxml")); 
    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.setResizable(false); 
    // the following solved it 
    stage.sizeToScene(); 
    stage.setTitle("JavaFx Test"); 
    stage.show(); 
} 

after after