【问题标题】:Possible to decide number of rows and columns in GridPane (JavaFX)可以决定 GridPane (JavaFX) 中的行数和列数
【发布时间】:2015-06-19 00:49:39
【问题描述】:

我想知道是否可以决定网格窗格应具有的行数和列数。

【问题讨论】:

  • 列数和行数取决于您添加的节点以及您在它们上设置的columnIndexrowIndex。您的问题不是很清楚:您能详细说明一下吗?
  • 我知道我的问题看起来很糟糕。我希望实现的是创建一个显示网格所有线条的网格基础布局。比如说我想创建一个 300 行的网格布局,然后我想将列跨度设置为 300。我希望这更有意义

标签: java javafx gridpane


【解决方案1】:

您可以将所需数量的ColumnConstraintsRowConstraints 添加到GridPane。例如:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class GridPaneForceColsAndRows extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setGridLinesVisible(true);
        final int numCols = 50 ;
        final int numRows = 50 ;
        for (int i = 0; i < numCols; i++) {
            ColumnConstraints colConst = new ColumnConstraints();
            colConst.setPercentWidth(100.0 / numCols);
            root.getColumnConstraints().add(colConst);
        }
        for (int i = 0; i < numRows; i++) {
            RowConstraints rowConst = new RowConstraints();
            rowConst.setPercentHeight(100.0 / numRows);
            root.getRowConstraints().add(rowConst);         
        }
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2019-01-30
    • 2021-03-17
    • 2016-10-29
    相关资源
    最近更新 更多