GridLayout coordinates

こ雲淡風輕ζ 提交于 2019-12-11 10:46:59

问题


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..


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/20286131/gridlayout-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!