【问题标题】: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网格窗格可以自动调整其对象的大小以适应吗?尝试将 max_width 和 max_height 设置为网格并让它调整内容大小。 JavaFX
【发布时间】:2012-04-07 11:34:18
【问题描述】:

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

我想在网格上设置 max_height 和 max_width 以及 min-width 和 min-height 并在我的窗口中将该网格居中。

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

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

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

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

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

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);
    }
}

【问题讨论】:

  • 我希望使用网格来放置“落”在网格列上的 png 图像。使用网格让我可以轻松地将 png 图像保持在适当的位置。有没有办法用网格做到这一点?

标签: java grid javafx javafx-2


【解决方案1】:

如果您想要一个可调整大小的矩形字段,则不需要网格。只需将它们放在窗格上并绑定到窗格大小:

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();
}

【讨论】:

  • 感谢 Sergey 我希望使用网格来放置“落”在网格列上的 png 图像。使用网格让我可以轻松地将 png 图像保持在适当的位置。有没有办法用网格做到这一点?
  • 你能详细说明一下“跌倒”吗?
猜你喜欢
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 2012-12-22
  • 2014-08-15
  • 1970-01-01
相关资源
最近更新 更多