JavaFX:调整窗口大小时调整窗格

时间:2018-12-11 22:36:46

标签: javafx layout resize gridpane pane

我正在尝试构建一个简单的Java项目,其中包含6个窗格(作为父级添加到GridPane Layout中)。我必须在开始时设置窗口大小,然后通过参考根目录布局的宽度和高度来设法将它们平均分配。

{
"status": "ok",
"sets": {
    "caseStudies": [
        {
        "_id": "1",
        "title": "Case Study Item One",
        "_title": "Case Study Item One",
        "date": "2018-06-01"
        }
    ]
 }
}

但是我希望它们在更改窗口大小时进行调整大小(使用鼠标,现在它们的大小已固定)。

所以我的问题是,如何在JavaFX中完成此操作?

>在调整窗口大小时,如何使元素拉伸并填充布局/布局的一部分,然后更改大小?

这是我的代码:

pane.setMinSize(root.getWidth()/3, root.getHeight()/2);

2 个答案:

答案 0 :(得分:1)

使用ColumnConstraintsRowConstraints来实现所需的调整大小行为:

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main6 extends Application {

    private static final int ROWS = 2, COLS = 3;

    private static final Color[][] colors = new Color[][] {
            {Color.GREEN, Color.WHITE, Color.GRAY},
            {Color.BLUE, Color.RED, Color.YELLOW}
    };

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane root = new GridPane();
        root.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

        //add panels 
        for(int row = 0; row < ROWS; row++){
            for(int col = 0; col < COLS; col++){
                root.getChildren().add( getPane(row, col));
            }
        }

        //Construct column constraints to resize horizontaly
        ObservableList<ColumnConstraints> colCconstraints = root.getColumnConstraints();
        colCconstraints.clear();
        for(int col = 0; col < COLS; col++){
             ColumnConstraints c = new ColumnConstraints();
             c.setHalignment(HPos.CENTER);
             c.setHgrow(Priority.ALWAYS);
             colCconstraints.add(c);
        }

        //Construct row constraints to resize vertically
        ObservableList<RowConstraints> rowCconstraints = root.getRowConstraints();
        rowCconstraints.clear();
        for(int row = 0; row < ROWS; row++){
             RowConstraints c = new RowConstraints();
             c.setValignment(VPos.CENTER);
             c.setVgrow(Priority.ALWAYS);
             rowCconstraints.add(c);
        }

        Scene scene = new Scene(root);
        primaryStage.setTitle("Learning JavaFX");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    /**
     *
     */
    private Pane getPane(int row, int col) {

        Pane pane = new Pane();
        pane.setBackground(new Background(new BackgroundFill(colors[row][col], CornerRadii.EMPTY, Insets.EMPTY)));
        pane.setPrefSize(300, 300);
        GridPane.setRowIndex(pane, row);
        GridPane.setColumnIndex(pane, col);

        return pane;
    }

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

答案 1 :(得分:0)

好的,我找到了部分解决方案。我添加了我的代码:

    ColumnConstraints column1 = new ColumnConstraints();
    column1.setPercentWidth(33.333333);
    ColumnConstraints column2 = new ColumnConstraints();
    column2.setPercentWidth(33.333333);
    ColumnConstraints column3 = new ColumnConstraints();
    column3.setPercentWidth(33.333333);
    root.getColumnConstraints().addAll(column1, column2, column3);

    RowConstraints row1 = new RowConstraints();
    row1.setPercentHeight(50);
    RowConstraints row2 = new RowConstraints();
    row2.setPercentHeight(50);
    root.getRowConstraints().addAll(row1, row2);

并删除了设置窗格大小的所有行,并允许调整内容的大小。

但是

当我将百分比宽度指定为(100/3)而不是33.3333时,它在窗口的右侧留下了一个奇怪的空白。有人可以向我解释为什么会这样吗?

看看:

enter image description here