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

JavaFX:调整窗口大小时调整窗格大小,javafx,layout,resize,gridpane,pane,Javafx,Layout,Resize,Gridpane,Pane,我正在尝试构建一个简单的Java项目,其中包含6个作为父级添加到GridPane布局的窗格。我必须在开始时设置窗口大小,并通过参考根布局的宽度和高度来平均分割它们 pane.setMinSize(root.getWidth()/3, root.getHeight()/2); 但是我想让它们在我用鼠标改变窗口大小时调整大小,现在它们的大小是固定的 所以我的问题是,如何在JavaFX中实现这一点?如何使元素拉伸并填充布局/布局的一部分,然后在调整窗口大小时更改大小 这是我的密码: import j

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

pane.setMinSize(root.getWidth()/3, root.getHeight()/2);
但是我想让它们在我用鼠标改变窗口大小时调整大小,现在它们的大小是固定的

所以我的问题是,如何在JavaFX中实现这一点?如何使元素拉伸并填充布局/布局的一部分,然后在调整窗口大小时更改大小

这是我的密码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class Main6 extends Application {

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

    GridPane root = new GridPane();
    Scene scene = new Scene(root, 900, 600);

    Pane pane1 = new Pane();
    pane1.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
    pane1.setMinSize(root.getWidth()/3, root.getHeight()/2);

    Pane pane2 = new Pane();
    pane2.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
    pane2.setMinSize(root.getWidth()/3, root.getHeight()/2);

    Pane pane3 = new Pane();
    pane3.setBackground(new Background(new BackgroundFill(Color.GREY, CornerRadii.EMPTY, Insets.EMPTY)));
    pane3.setMinSize(root.getWidth()/3, root.getHeight()/2);

    Pane pane4 = new Pane();
    pane4.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
    pane4.setMinSize(root.getWidth()/3, root.getHeight()/2);

    Pane pane5 = new Pane();
    pane5.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
    pane5.setMinSize(root.getWidth()/3, root.getHeight()/2);

    Pane pane6 = new Pane();
    pane6.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
    pane6.setMinSize(root.getWidth()/3, root.getHeight()/2);

    root.getChildren().addAll(pane1, pane2, pane3, pane4, pane5, pane6);
    GridPane.setRowIndex(pane1, 0);
    GridPane.setColumnIndex(pane1, 0);
    GridPane.setRowIndex(pane2, 0);
    GridPane.setColumnIndex(pane2, 1);
    GridPane.setRowIndex(pane3, 0);
    GridPane.setColumnIndex(pane3, 2);
    GridPane.setRowIndex(pane4, 1);
    GridPane.setColumnIndex(pane4, 0);
    GridPane.setRowIndex(pane5, 1);
    GridPane.setColumnIndex(pane5, 1);
    GridPane.setRowIndex(pane6, 1);
    GridPane.setColumnIndex(pane6, 2);

    primaryStage.setTitle("Learning JavaFX");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

好吧,我找到了部分解决办法。我在代码中添加了:

    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时,窗口右侧会留下一个奇怪的空白。谁能给我解释一下为什么会这样

看一看:

使用ColumnConstraints和RowConstraints实现所需的调整大小行为:

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);
    }
}

您可能需要一个VBox或HBox.100/3,这将导致33。如果想要得到正确的结果,其中一个数字必须是双精度的:100.0/3.0。@Slaw谢谢!当然这是一个整数!非常感谢。非常感谢!我来分析一下: