当前位置: 首页 > 知识库问答 >
问题:

保持Gridpane的高度和宽度(纵横比)相同

阳建弼
2023-03-14

我有一个javafx GridPane,当我调整阶段大小时,它会调整自己的大小以适应阶段的大小。我希望gridpane的高度与gridpane的宽度相匹配,并因此将大小调整为尽可能大的大小,同时保持这种约束。

我不得不在GridPane的父级中添加更多的元素。我发现添加的元素完全没有正常的行为,并且相互重叠。

当然,当我移除重写方法时,它们不再重叠,但当我调整舞台大小时,网格窗格并没有保持一个完美的正方形。

我想到尝试在后台使用Image/ImageView,并为其应用setPreserveratio(true)。然后将此图像的heightProperty绑定到GridPane的prefHeightProperty上,但由于某种原因,这也不会给我带来任何结果。

这是第二种方法的一个MCVE,它不起作用,但如果我能以某种方式使它起作用,将是很好的。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import html" target="_blank">javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane stackPane = new StackPane();

        Image image = new Image("Square Image.png", 300, 300, true, true);
        ImageView imageView = new ImageView(image);
        // This makes the image resize while maintaining its square shape...
        imageView.fitHeightProperty().bind(stackPane.heightProperty());
        imageView.fitWidthProperty().bind(stackPane.widthProperty());
        imageView.setPreserveRatio(true);

        GridPane gridPane = new GridPane();
        for(int i=0; i<5; i++) {
            for (int j = 0; j < 5; j++) {
                Pane pane = new Pane();
                pane.setPrefSize(100, 100);
                gridPane.add(pane, i, j);
            }
        }
        // Does not work as intended... :(
        gridPane.prefWidthProperty().bind(imageView.fitWidthProperty());
        gridPane.prefHeightProperty().bind(imageView.fitHeightProperty());
        
        /*
        Tried this as well, also does not work.. :(
        gridPane.prefWidthProperty().bind(image.widthProperty());
        gridPane.prefHeightProperty().bind(image.heightProperty());
        */


        gridPane.setGridLinesVisible(true);

        stackPane.getChildren().addAll(imageView, gridPane);
        primaryStage.setScene(new Scene(stackPane));
        primaryStage.show();
    }
  
    public static void main(String[] args) {
        launch(args);
    }
}

当然,所有这些方法都是试图实现这一目标的廉价、简陋的方法。如果有一个标准的方法做它,但我不知道,那么我想知道,取而代之。

共有1个答案

越昊穹
2023-03-14

GridPane封装在StackPane中,并使用绑定动态更改GridPane子级首选大小,同时保持纵横比:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;

    public class Main extends Application {

        private final int SIZE = 5;

        @Override
        public void start(Stage primaryStage) {

            GridPane gridPane = new GridPane();
            gridPane.setAlignment(Pos.CENTER);

            StackPane centerPane = new StackPane(gridPane);
            centerPane.setStyle("-fx-background-color:cyan");
            centerPane.setPrefSize(SIZE*50, SIZE*50);

            for(int i=0; i<SIZE; i++) {
                for (int j = 0; j < SIZE; j++) {
                    Pane pane = new Pane();
                    pane.setStyle("-fx-background-color:red");
                    gridPane.add(pane, i, j);
                    pane.prefWidthProperty().bind(Bindings.min(centerPane.widthProperty().divide(SIZE),
                                                                centerPane.heightProperty().divide(SIZE)));
                    pane.prefHeightProperty().bind(Bindings.min(centerPane.widthProperty().divide(SIZE),
                                                                centerPane.heightProperty().divide(SIZE)));
                }
            }
            gridPane.setGridLinesVisible(true);

            primaryStage.setScene(new Scene(centerPane));
            primaryStage.show();
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
 类似资料:
  • (在stackoverflow上已经提出了类似的问题,但这个问题有更多的限制,例如特定的最大宽度、最大高度、所需的特定高度和宽度,并且没有布局偏移。) 我想要一个具有以下约束的响应图像: 最大宽度:100%,这样它就不会溢出到右侧,并且在减小屏幕宽度时会有响应 如何使用CSS实现这一点? (如果CSS无法实现这一点,那么可能是在JavaScript中?) 我尝试了几个CSS功能,例如对象适合度和最

  • 使用CSS flex box模型,如何强制图像保持其纵横比? JS小提琴:http://jsfiddle.net/xLc2Le0k/2/ 请注意,为了填充容器的宽度,图像会拉伸或收缩。这很好,但是我们也能让它拉伸或收缩高度来保持图像比例吗? HTML CSS

  • 我在响应式站点上设置了flexSlider。我正在创建的滑块可以拍摄横向和纵向图像。我设置了,以便调整高度以适应图像。 在最大的媒体查询中,flexslider容器的宽度为750px。这对于横向图像很好,但纵向图像太大了。肖像图像的宽度设置为与容器相同的大小-750px宽,因此高度至少是容器宽度的两倍,因此如果不滚动,则无法查看整个图像。 在css中,我尝试设置最大高度:750px,这解决了高度问

  • 我有一个。的数据是从服务器请求的。 以下是中的项布局: 我从服务器请求数据,获取图像url并将图像加载到 适配器中有方法的代码 图像大小为。我在我的Nexus4上运行我的应用程序。图像填充宽度,但高度不缩放。不显示整个图像。 我如何解决这个问题?

  • 问题内容: 目前,通过STYLE,我可以在和高度上使用和,反之亦然,但是我仍然不能将图像分别限制在一个特定的位置,无论是太宽还是太高。 有任何想法吗? 问题答案: 如果仅在图像上定义一维,则将始终保留图像长宽比。 图像是否比您喜欢的更大/更佳? 您可以将其放入DIV中,该DIV设置为图像所需的最大高度/宽度,然后设置“ overflow:hidden”。那将收获超出您想要的任何东西。 如果图像的宽

  • 我在TableViewCell中显示了拉伸图像。我需要找到图像的高度和宽度从图像URL。我需要找到纵横比,并根据Swift4中动态单元格的高度在单元格上拟合图像。