Javafx 8:GridPane的高度不适合父VBox

时间:2018-11-02 07:45:33

标签: javafx gridpane hbox

我无法正确地将GridPane设置为父VBox的高度。尽管我设置了行约束,但VBox的垂直方向在gridpane上增长为ALWAYS,vbox setfillwidth为true,当VBox高度增长时,其上部区域变为空。 这是我的代码。任何帮助都是有价值的。

公共类Kryptolexo1扩展了应用程序{

final static int ROWS = 18;
final static int CLMNS = 18;
final static int HWORDS = 8;
final static int VWORDS = 7;
final static KLabel[][] label = new KLabel[ROWS][CLMNS];
final static Color selectedColor = Color.AQUA;
static KLabel getLabel(int r, int c) {
    return label[r][c];
}


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

    AnchorPane root = new AnchorPane();

    VBox vBox = new VBox();
    AnchorPane.setBottomAnchor(vBox, 0.);
    AnchorPane.setLeftAnchor(vBox, 0.);
    AnchorPane.setRightAnchor(vBox, 0.);
    AnchorPane.setBottomAnchor(vBox, 0.);

    vBox.setFillWidth(true);

    GridPane gridPane = new GridPane();
    gridPane.setGridLinesVisible(true);

    ColumnConstraints cC = new ColumnConstraints();
    cC.setHgrow(Priority.ALWAYS);
    for (int i = 0; i < CLMNS; i++) {
        gridPane.getColumnConstraints().add(i, cC);
    }

    RowConstraints rC=new RowConstraints();
    rC.setVgrow(Priority.ALWAYS);
    rC.setFillHeight(true);
    rC.setMaxHeight(Double.MAX_VALUE);
    rC.setMinHeight(20);
    //rC.setPercentHeight(100);
    for (int i = 0; i < ROWS; i++) {
        gridPane.getRowConstraints().add(i, rC);
    }
    VBox.setVgrow(gridPane, Priority.ALWAYS);

    Label messageArea = new Label();
    VBox.setVgrow(messageArea, Priority.ALWAYS);
    messageArea.setMinHeight(60);
    messageArea.setMaxWidth(Double.MAX_VALUE);
    messageArea.setMaxHeight(Double.MAX_VALUE);
    messageArea.setStyle("-fx-background-color: YELLOW;");

    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < CLMNS; j++) {
            label[i][j] = new KLabel();
        }
    }
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < CLMNS; j++) {
            getLabel(i, j).getLabel().setFont(Font.font(Font.getDefault().getName(), FontWeight.EXTRA_BOLD, 18.));
            getLabel(i, j).getLabel().setPadding(new Insets(2, 4, 2, 4));
            GridPane.setValignment(getLabel(i, j).getLabel(), VPos.CENTER);
            GridPane.setHalignment(getLabel(i, j).getLabel(), HPos.CENTER);

            gridPane.add(getLabel(i, j).getLabel(), j, i);

            //getLabel(i, j).getLabel().setText(Character.toString(c++));
        }
    }

    gridPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    vBox.getChildren().addAll(gridPane, messageArea);

    root.getChildren().add(vBox);
    Scene scene = new Scene(root);

    primaryStage.setTitle("Kryptolexo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);

}

0 个答案:

没有答案