【问题标题】:How To Add Objects In Grid from Bottom Up?如何自下而上在网格中添加对象?
【发布时间】:2019-08-31 08:02:56
【问题描述】:

我使用网格窗格视图使用 add() 方法填充网格。通过 add() 的设计,网格从上到下填充每一行,其中位置 (0,0) 位于网格的最左上角。添加更多行然后附加在它下面,依此类推。是否可以填充我的网格以使第一行位于底部并向上添加行以使位置 (0,0) 位于左下角?实现这一目标需要什么?我查看了 GridPane 中的不同方法,但找不到这是如何完成的。我的预感是我需要重写 add() 方法,但我不确定如何实现此行为。

我不想镜像这个,因为我正在处理图像。

为简单起见,以下是从类和辅助方法中提取的代码要点:

public enum LawnType
{
    GRASS,
    CRATER
}

lawnData = new LawnType[][] {
        {CRATER,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
    };

GridPane lawnViewer = new GridPane();

for (int x = 0 ; x < data.length ; x++) {
    for (int y = 0 ; y < data[x].length ; y++) {
        ImageView imageView;

        switch(data[x][y]){
            case GRASS:
                imageView = new ImageView(new Image("mower/resources/longgrass1.png"));
                imageView.setFitWidth(gridPixelSize);
                imageView.setFitHeight(gridPixelSize);
                gridPane.add(imageView,x,y);
                break;
            case CRATER:
                imageView = new ImageView(new Image("mower/resources/crater.png"));
                imageView.setFitWidth(gridPixelSize);
                imageView.setFitHeight(gridPixelSize);
                gridPane.add(imageView, x, y);
                break;
            default:
                break;
            }
        }
    }

输出:

【问题讨论】:

  • gridPane.add(imageView, x, data[x].length - 1 - y)?
  • 为此创建一个答案并接受它。
  • @VGR 做一些家务。你能回答一下,这样我就可以选择解决了吗?谢谢!

标签: java javafx gridpane


【解决方案1】:

您可以将每个图像添加到相对于底行的一行,即data[x].length - 1

替换其中的每一个:

gridPane.add(imageView, x, y);

用这个:

gridPane.add(imageView, x, data[x].length - 1 - y)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多