【问题标题】:getting current width and height of Gridpane获取 Gridpane 的当前宽度和高度
【发布时间】:2020-03-04 13:55:16
【问题描述】:
    imageGrid = new GridPane();
    imageGrid.prefWidthProperty().bind(gallerie.widthProperty().multiply(1.00));
    imageGrid.prefHeightProperty().bind(gallerie.heightProperty().multiply(0.9));
    imageGrid.setPadding(new Insets(20, 20, 20, 20));
    imageGrid.setHgap(20);
    imageGrid.setVgap(20);
    imageGrid.setStyle("-fx-background-color: #154BE5");

... ... ...

static void loadCurrentImages(ArrayList<File> imageFiles) {

    ArrayList<ImageView> ivs = new ArrayList<>();
    int i = 0;
    int k = 0;
    int j = 0;
    for (File f : imageFiles) {
        ImageView iv = new ImageView();
        iv.setFitWidth(100);
        iv.setPreserveRatio(true);
        iv.setSmooth(true);
        iv.setImage(new Image(f.toURI().toString()));
        ivs.add(iv);
        imageGrid.add(ivs.get(i), k, j);
        i++;
        k++;
        if(k > imageGrid.widthProperty().intValue() / 100){  //???????
            k = 0;
            j++;
        }
    }
}

在最后一个 if 子句中,我想使用网格的当前大小,但我不知道如何设置它,因为它本身绑定到另一个大小(画廊)

我想得到当前的int值

【问题讨论】:

  • 您确定不想要FlowPane 吗?如果您的节点都具有相同的宽度,则此布局应相当于 GridPane 加上自动换行的“行”,如果 FlowPane 调整大小,则会自动更新自身。此外,如果galerieimageGrid 有一个共同的父级,则可能有比绑定更好的方法来保持宽度相同。选择适当的布局作为祖先...

标签: javafx width gridpane


【解决方案1】:

节点的布局边界不会立即计算。仅当在场景图中渲染节点时才会计算。要根据需要获得它们,您需要明确请求布局。

node.applyCss();
node.requestLayout();

请注意,使用上述两行可能会对性能产生轻微影响。

忽略所有代码问题(静态、命名约定、实际逻辑..),下面是您可以尝试的快速想法。

static void loadCurrentImages(ArrayList<File> imageFiles) {

    ArrayList<ImageView> ivs = new ArrayList<>();
    int i = 0;
    int k = 0;
    int j = 0;
    for (File f : imageFiles) {
        ImageView iv = new ImageView();
        iv.setFitWidth(100);
        iv.setPreserveRatio(true);
        iv.setSmooth(true);
        iv.setImage(new Image(f.toURI().toString()));
        ivs.add(iv);
        imageGrid.add(ivs.get(i), k, j);
        i++;
        k++;

        // Requesting layout
        imageGrid.applyCss();
        imageGrid.requestLayout();

        if(k > imageGrid.widthProperty().intValue() / 100){  //???????
            k = 0;
            j++;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2021-06-30
    相关资源
    最近更新 更多