GridLayout coordinates

2019-08-02 02:38发布

So I think you will understand my problem by this piece of code:

int s = 4;
int v = 4;    

world.setLayout(new GridLayout(s, v));

        grid = new JLabel[s][v];

        for (int x = s-1; x >= 0; x--) {

            for (int y = 0; y < v; y++) {

                grid[x][y] = new JLabel((x)+","+(y));

                world.add(grid[x][y]);

Now I get a grid with coordinates:

3,0  3,1  3,2  3,3
2,0  2,1  2,2  2,3
1,0  1,1  1,2  1,3
0,0  0,1  0,2  0,3

But I would like to get:

0,3  1,3  2,3  3,3
0,2  1,2  2,2  3,2
0,1  1,1  2,1  3,1
0,0  1,0  2,0  3,0

Any help appreciated..

2条回答
Lonely孤独者°
2楼-- · 2019-08-02 03:08

I didn't test it but try this out:

    for (int y = s-1; y >= 0; y--) {

        for (int x = 0; x < v; x++) {

            grid[x][y] = new JLabel((x)+","+(y));

            world.add(grid[x][y]);
查看更多
beautiful°
3楼-- · 2019-08-02 03:11

I did not test it, but try change your code:

grid[x][y] = new JLabel((x)+","+(y));

to:

grid[x][y] = new JLabel((y)+","+(x));
查看更多
登录 后发表回答