JavaFX GridPane 调整窗格子项的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22298336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:48:56  来源:igfitidea点击:

JavaFX GridPane resizing of pane children

javajavafxjavafx-2

提问by GGrec

This maybe a duplicate question, but I haven't found a solution that suits my needs.

可能是一个重复的问题,但我还没有找到适合我需求的解决方案。

Inspired from jewelsea's ColorChooserSample, half-way through my implementation I realised that manual size can only be set on Controls, and not on Panes. These panes should be smart enough to resize themselves automatically depending on their parent.

从启发jewelseaColorChooserSample,中途通过我实现我意识到的手动大小只能被设置ControlS,而不是Pane秒。这些窗格应该足够智能,可以根据其父级自动调整大小。

Is there a way for the GridPanes children to grab vertical and horizontal space at the same time? Much like a VBoxand HBoxcombined. I bet the solution involves AnchorPane.

有没有办法让GridPanes孩子同时抓取垂直和水平空间?很像一个VBoxHBox组合。我敢打赌解决方案涉及AnchorPane.

Keep in my the children are panesand not controls.

保持在我的孩子是窗格不是控件



SSCCE with buttons(copy - paste - run - resize window)

带按钮的 SSCCE(复制 - 粘贴 - 运行 - 调整窗口大小)

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 
 * @author ggrec
 *
 */
public class DashboardFXGrid extends Application
{

    // ==================== 1. Static Fields ========================

    private final static double GOLDEN_RATIO = 1.618;

    private final static double MIN_TILE_SIZE = 5;
    private final static double MAX_TILE_SIZE = Double.MAX_VALUE;


    // ====================== 2. Instance Fields =============================

    private DoubleProperty prefTileSize = new SimpleDoubleProperty(MIN_TILE_SIZE);

    private double nColumns;
    private double nRows;

    private GridPane gridPane;


    // ==================== 3. Static Methods ====================

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


    // ==================== 4. Constructors ====================

    @Override
    public void start(final Stage primaryStage) throws Exception
    {
        gridPane = new GridPane();

        gridPane.setPadding(new Insets(20));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        nColumns = Math.floor(Math.sqrt(dummyButtons().length) * 2 / GOLDEN_RATIO);
        nRows    = Math.ceil(dummyButtons().length / nColumns);

        createContents();

        addResizeListeners();

        primaryStage.setScene(new Scene(gridPane));
        primaryStage.show();
    }


    // ==================== 5. Creators ====================

    private void createContents()
    {
        int i = 0;

        for (final Button button : dummyButtons())
        {
            GridPane.setRowIndex(button,       i / (int) nColumns);
            GridPane.setColumnIndex(button, i % (int) nColumns);

            button.setMinSize(MIN_TILE_SIZE, MIN_TILE_SIZE);
            button.setMaxSize(MAX_TILE_SIZE, MAX_TILE_SIZE);

            gridPane.getChildren().add(button);

            i++;
        }
    }

    private void addResizeListeners()
    {
        gridPane.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {

            @Override public void changed(final ObservableValue<? extends Bounds> observableValue, final Bounds oldBounds, final Bounds newBounds)
            {
                prefTileSize.set(Math.max(MIN_TILE_SIZE, Math.min(newBounds.getWidth() / nColumns, newBounds.getHeight() / nRows)));

                for (final Node child : gridPane.getChildrenUnmodifiable())
                {
                    final Control tile = (Control) child;
                    tile.setPrefSize(prefTileSize.get(), prefTileSize.get());
                }
            }
        });
    }


    // ==================== 15. Other ====================

    private static final Button[] dummyButtons()
    {
        final Button[] buttons = new Button[5];

        for(int i = 0; i < buttons.length; i++)
        {
            buttons[i] = new Button(String.valueOf(i));
        }

        return buttons;
    }

}

采纳答案by Uluk Biy

Is there a way for the GridPanes children to grab vertical and horizontal space at the same time?

有没有办法让 GridPanes 孩子同时抓取垂直和水平空间?

Try to use RowConstraintsand ColumnConstraintsof GridPane:

尝试使用RowConstraintsColumnConstraintsGridPane:

for (final Button button : dummyButtons()) {
    GridPane.setRowIndex(button, i / (int) nColumns);
    GridPane.setColumnIndex(button, i % (int) nColumns);

    button.setMinSize(MIN_TILE_SIZE, MIN_TILE_SIZE);
    button.setMaxSize(MAX_TILE_SIZE, MAX_TILE_SIZE);

    gridPane.getChildren().add(button);

    i++;
}

for (int j = 0; j < nColumns; j++) {
    ColumnConstraints cc = new ColumnConstraints();
    cc.setHgrow(Priority.ALWAYS);
    gridPane.getColumnConstraints().add(cc);
}

for (int j = 0; j < nRows; j++) {
    RowConstraints rc = new RowConstraints();
    rc.setVgrow(Priority.ALWAYS);
    gridPane.getRowConstraints().add(rc);
}

and without calling addResizeListeners().

并且无需调用addResizeListeners()