2014-01-06 47 views
4

我在ScrollPane中有一個FlowPane。在流程窗格中,我放置了四個矩形形狀。 當我調整(降低)窗口滾動窗格中沒有顯示出滾動條,如下圖所示:如何在ScrollPane大小調整時使滾動條顯示? (在JavaFX中)

enter image description here

但它在某些時候出現了,當我減少規模。

下面是我的代碼:

public class FlowPaneInsideScrollPane extends Application{ 
public static void main(String[] args) { 
    launch(args); 
} 
@Override 
public void start(Stage primaryStage) throws Exception { 

    ScrollPane scrollPane = new ScrollPane(); 
    scrollPane.setFitToWidth(true); 
    scrollPane.setFitToHeight(true); 

    FlowPane flowPane = new FlowPane(); 
    flowPane.setStyle("-fx-background-color: blue");   
    flowPane.setPadding(new Insets(10, 10, 10, 10)); 
    flowPane.setHgap(10); 
    flowPane.setVgap(10); 

    Rectangle rect1 = new Rectangle(100, 100, Color.RED); 
    Rectangle rect2 = new Rectangle(100, 100, Color.RED); 
    Rectangle rect3 = new Rectangle(100, 100, Color.RED); 
    Rectangle rect4 = new Rectangle(100, 100, Color.RED); 

    flowPane.getChildren().add(rect1); 
    flowPane.getChildren().add(rect2); 
    flowPane.getChildren().add(rect3); 
    flowPane.getChildren().add(rect4); 

    scrollPane.setContent(flowPane);  
    primaryStage.setScene(new Scene(scrollPane, 500, 500));  
    primaryStage.show();   
} 
} 

I)的滾動窗格的財產我應該檢查,以獲得用於顯示滾動條的缺省值? ii)如何修改代碼以在所需點(調整大小時)顯示滾動條?

+0

您正在使用哪個JDK版本? –

+0

我正在使用JDK 1.7 – Angom

回答

7

ScrollBar策略由ScrollPane.setHbarPolicy(...)ScrollPane.setVbarPolicy(...)決定。

我認爲這裏的問題是你有scrollPane.setFitToWidth(true)scrollPane.setFitToHeight(true),這導致ScrollPane試圖強制FlowPane的大小與滾動窗格的視口相同。嘗試註釋掉scrollPane.setFitToHeight(true)。如果需要,可以將scrollPane的背景顏色設置爲流程窗格的相同背景顏色。

+0

謝謝James,評論scrollPane.setFitToHeight(true)的作品。 – Angom

相關問題