【问题标题】:How to set a GridPane to adjust automatically to content in JavaFX如何设置 GridPane 以自动调整到 JavaFX 中的内容
【发布时间】:2016-07-03 06:03:42
【问题描述】:

我在ScrollView 中有一个GridPane。我在GridPane 的每个空间中添加图像。首先只有 1 行 3 列。但是当我传递 3 张图像的数量时,我将GridPane 增加一行,并添加表示网格行高度的数字 (130px)。所以这是我的代码。

 private void AddItem() {
        ImageView image = new ImageView();
        image.setImage(new Image("media/logo.jpg"));
        image.setFitHeight(50);
        image.setFitWidth(100);
        if (columns > 2) {//when it reaches the end of the columns
            viewGrid.setPrefHeight(viewGrid.getHeight() + 130);//I increase the height of the GridPane
            viewGrid.addRow(rows++);//I add a new row
            columns=0;//I set the column to 0 again
        }

        viewGrid.add(image, columns, rows);
        columns++;
 }

结果

图像开始相互碰撞,而不是保持真实位置:

GridPane 中是否有一些属性可以让我实现所需的行为?

【问题讨论】:

    标签: java gridview javafx scrollview


    【解决方案1】:

    不要手动调整GridPane 的大小。请改用rowConstraints。这些允许您指定行的属性,例如对齐和大小约束。 GridPane 会自动更新它的高度。

    示例

    private static final int COLUMN_COUNT = 3;
    private int nextColumnIndex = COLUMN_COUNT;
    private int currentRow = -1;
    
    @Override
    public void start(Stage primaryStage) {
        Image image = new Image("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a");
        
        GridPane imagePane = new GridPane();
        
        ColumnConstraints colConstraint = new ColumnConstraints(120);
        colConstraint.setHalignment(HPos.LEFT);
        
        RowConstraints rowConstraints = new RowConstraints(130);
        rowConstraints.setValignment(VPos.CENTER);
        
        // add constraints for columns
        imagePane.getColumnConstraints().addAll(colConstraint, colConstraint, colConstraint);
    
        ScrollPane scroll = new ScrollPane(imagePane);
        scroll.setFitToWidth(true);
        scroll.setPrefSize(380, 300);
    
        BorderPane root = new BorderPane(scroll);
    
        Button btn = new Button("Add Image");
        btn.setOnAction((ActionEvent event) -> {
            ImageView newImage = new ImageView(image);
            newImage.setFitHeight(50);
            newImage.setFitWidth(100);
            if (nextColumnIndex >= COLUMN_COUNT) {
                nextColumnIndex = 0;
                currentRow++;
                imagePane.getRowConstraints().add(rowConstraints);
            }
            imagePane.addRow(currentRow, newImage);
            nextColumnIndex++;
        });
    
        root.setLeft(new VBox(btn));
    
        Scene scene = new Scene(root);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    顺便说一句:addRow 方法用于将子代添加到特定行,而不是用于添加行本身。如果将带有索引(或更大索引)的Node 添加为子项,则会自动将一行添加到GridPane。您无需通过调用GridPane 方法来创建行。


    请注意,与您尝试实现的行为类似的行为已在 FlowPane 中实现。这将是简化代码的一个选项。只需确保正确选择 FlowPane 的宽度(即允许连续 3 个,但不允许 4 个元素)。

    示例

    @Override
    public void start(Stage primaryStage) {
        Image image = new Image("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a");
    
        FlowPane imagePane = new FlowPane();
        imagePane.setHgap(20);
        imagePane.setVgap(130 - 50);
        imagePane.setRowValignment(VPos.CENTER);
        imagePane.setColumnHalignment(HPos.LEFT);
    
        ScrollPane scroll = new ScrollPane(imagePane);
        scroll.setFitToWidth(true);
        scroll.setPrefSize(380, 300);
    
        BorderPane root = new BorderPane(scroll);
    
        Button btn = new Button("Add Image");
        btn.setOnAction((ActionEvent event) -> {
            ImageView newImage = new ImageView(image);
            newImage.setFitHeight(50);
            newImage.setFitWidth(100);
            imagePane.getChildren().add(newImage);
        });
    
        root.setLeft(new VBox(btn));
    
        Scene scene = new Scene(root);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

    • 我试过这样做,它比其他方式好得多。使用此解决方案,我不需要使用索引,因此它变得更加容易。谢谢。
    猜你喜欢
    • 2016-10-04
    • 2016-09-30
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2014-07-02
    • 2016-01-12
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多