一个gridpane可以自动调整它的对象的大小吗?尝试将max_width和max_height设置为网格并使其调整内容大小。 JavaFX的 [英] Can a gridpane automatically resize it's objects to fit? Trying to set a max_width and max_height to a grid and have it resize content. JavaFX

查看:1052
本文介绍了一个gridpane可以自动调整它的对象的大小吗?尝试将max_width和max_height设置为网格并使其调整内容大小。 JavaFX的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。一个网格可以自动调整其中的对象以适应网格吗?



我想在网格上设置max_height和max_width以及min-width和min-高度和中心网格在我的窗口中。



在我的网格中,我想在所有位置添加Spacer Square。每个垫片都有边框。所以它看起来像一个实际的网格。



如果我的网格是4x4,我希望那里有16个大方格。


$ b $如果我的网格是16x18,我希望有288平方。



这两个选项都应占用一定数量的区域,288平方选项正方形比16选项小很多,以适应我的网格尺寸。



我检查了gridpane文档,但是如果对我有选项,我很困惑。我不知道填充,边距,setmaxwidth,setmaxheight之间的区别(试过这个,没有改变的东西)。

  double dimension_x = 100; //实际平方/间隔/网格空间的最大宽度
double dimension_y = 100; // max_height实际平方/ spacer / gridspace

int grid_x = 100; //行数
int grid_y = 100; //列数
Rectangle [] []矩形=新Rectangle [grid_x] [grid_y];

GridPane grid = new GridPane();
double grid_max_x = 800;
double grid_max_y = 600;
grid.setHgap(1);
grid.setVgap(1);
grid.setPadding(new Insets(16)); //不知道这是什么。尝试失败
grid.setEffect(addEffect(Color.web(#202C2F),.61,12));
grid.setMaxHeight(grid_max_y); (int x = 0; x {
for(int y = 0; y< grid_y; y ++)
{
Rectangle temp = new Rectangle(dimension_x,dimension_y);
grid.add(temp,x,y);
}
}


解决方案

你想要一个可调整大小的矩形区域,而不需要网格。只需将它们放在窗格上并绑定到窗格大小即可:

  public void start(Stage stage){
Pane root =新窗格();

final int count = 7; //数字矩形

NumberBinding minSide =绑定
.min(root.heightProperty(),root.widthProperty())
.divide(count); (int x = 0; x for(int y = 0; y 矩形矩形=新的

矩形(0,0,Color.LIGHTGRAY);

rectangle.xProperty()。bind(minSide.multiply(x));
rectangle.yProperty()。bind(minSide.multiply(y));
rectangle.heightProperty()。bind(minSide.subtract(2));
rectangle.widthProperty()。bind(rectangle.heightProperty());
root.getChildren()。add(rectangle);



stage.setScene(new Scene(root,500,500));
stage.show();
}


I have a simple question. Can a grid automatically resize the objects in it to fit the grid?

I want to set a max_height and max_width on a grid as well as a min-width and min-height and center that grid in my window.

In my grid, I want to add Spacer Square in all spots. Each spacer has a border. So it will look like an actual grid.

If my grid is 4x4, I want there to be 16 big squares.

If my grid is 16x18, I want there to be 288 squares.

Both options should take up an area of a set amount, the 288 square option having squares a lot smaller than the 16 option in order to fit my grid dimensions.

I checked the gridpane documentation, but am confused if there is an option for me. I don't know the difference between padding, margins, setmaxwidth, setmaxheight (tried this, didnt change a thing).

double dimension_x=100; //max_width of actual square/spacer/gridspace
double dimension_y=100; //max_height of actual square/spacer/gridspace

int grid_x=100; //number of rows
int grid_y=100; //number of columns
Rectangle[][] rectangles = new Rectangle[grid_x][grid_y];

GridPane grid = new GridPane();
double grid_max_x=800;
double grid_max_y=600;
grid.setHgap(1);
grid.setVgap(1);
grid.setPadding(new Insets(16)); //not sure what this does. Attempt Fail
grid.setEffect(addEffect(Color.web("#202C2F"), .61, 12));
grid.setMaxHeight(grid_max_y); //does nothing that it APPEARS to me

for (int x=0;x<grid_x;x++)
{
    for(int y=0;y<grid_y;y++)
    {
        Rectangle temp = new Rectangle(dimension_x,dimension_y);
        grid.add(temp,x,y);
    }
}

解决方案

If you want a field of resizable rectangles you don't need a grid. Just put them on Pane and bind to the pane size:

public void start(Stage stage) {
    Pane root = new Pane();

    final int count = 7; //number of rectangles

    NumberBinding minSide = Bindings
            .min(root.heightProperty(), root.widthProperty())
            .divide(count);

    for (int x = 0; x < count; x++) {
        for (int y = 0; y < count; y++) {
            Rectangle rectangle = new Rectangle(0, 0, Color.LIGHTGRAY);

            rectangle.xProperty().bind(minSide.multiply(x));
            rectangle.yProperty().bind(minSide.multiply(y));
            rectangle.heightProperty().bind(minSide.subtract(2));
            rectangle.widthProperty().bind(rectangle.heightProperty());
            root.getChildren().add(rectangle);
        }
    }

    stage.setScene(new Scene(root, 500, 500));
    stage.show();
}

这篇关于一个gridpane可以自动调整它的对象的大小吗?尝试将max_width和max_height设置为网格并使其调整内容大小。 JavaFX的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆