自动调整为父级

时间:2016-02-05 20:50:15

标签: javafx javafx-8

我有这个简单的例子。我想自动调整图表以填充父母。

@Override
    public void start(Stage primaryStage)
    {

        FlowPane flowPane = new FlowPane(Orientation.HORIZONTAL);

        flowPane.getChildren().addAll(test());
        flowPane.setPadding(new Insets(5, 5, 5, 5));
        flowPane.setStyle("-fx-background-color: green;");
        //flowPane.setPrefWidth(1200);
        flowPane.setVgap(4);
        flowPane.setHgap(4);
        flowPane.setAlignment(Pos.TOP_LEFT);

        ScrollPane scroll = new ScrollPane();
        scroll.setStyle("-fx-background-color: transparent;");
        scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Horizontal scroll bar
        scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Vertical scroll bar
        scroll.setFitToHeight(true);
        scroll.setFitToWidth(true);
        scroll.setContent(flowPane);

        Scene scene = new Scene(scroll, 1500, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }

    private AreaChart<String, Number> test()
    {
        CategoryAxis xAxis = new CategoryAxis();

        NumberAxis yAxis = new NumberAxis();

        final AreaChart<String, Number> sc = new AreaChart<>(xAxis, yAxis);
        sc.setAnimated(false);  // Turn off chart animation during first appearance

        sc.setCreateSymbols(false);

        sc.setPrefSize(1200, 210);

        return sc;
    }

如何自动调整图表大小以填充父级大小?有没有绑定的简单解决方案?

你能提出一些解决方案吗?

1 个答案:

答案 0 :(得分:0)

所以,既然你想用一张图表填充整个父母,我不认为有任何理由使用FlowPane。最好使用StackPane代替

public void start(Stage primaryStage)
{

    StackPane stackPane = new StackPane();
    AreaChart<String, Number> chart = test();
    StackPane.setAlignment(chart, Pos.CENTER);
    stackPane.getChildren().add(chart);
    stackPane.setStyle("-fx-background-color: green");

//  FlowPane flowPane = new FlowPane(Orientation.HORIZONTAL);
//
//  flowPane.getChildren().addAll(test());
//  flowPane.setPadding(new Insets(5, 5, 5, 5));
//  flowPane.setStyle("-fx-background-color: green;");
//  //flowPane.setPrefWidth(1200);
//  flowPane.setVgap(4);
//  flowPane.setHgap(4);
//  flowPane.setAlignment(Pos.TOP_LEFT);

    ScrollPane scroll = new ScrollPane();
    scroll.setStyle("-fx-background-color: transparent;");
    scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Horizontal scroll bar
    scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Vertical scroll bar
    scroll.setFitToHeight(true);
    scroll.setFitToWidth(true);
//  scroll.setContent(flowPane);
    scroll.setContent(stackPane);

    Scene scene = new Scene(scroll, 1500, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

但是,如果您真的想使用FlowPane,那么您可以使用绑定。在你的情况下,它很容易。它只是简单的2行代码

    chart.prefWidthProperty().bind(flowPane.widthProperty().subtract(10)); //substract 10 because of 5px insets on each side
    chart.prefHeightProperty().bind(flowPane.heightProperty().subtract(10)); //same here

这里你只是说chart的首选宽度和高度总是等于flowPane的宽度和高度减去10(因为你的插图)

相关问题