How to get X and Y index of element inside GridLayout?

后端 未结 5 2094
不知归路
不知归路 2020-11-22 01:12

I am studying a java tutorial and saw that the way to find the x/y indexes of a JButton inside a GridLayout is to traverse a bidimensional array of buttons b which is associ

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:44

    You can use setName() to store within a JButton its location(ex. button.setName(i+" "+j);) when you create it; you can then access it by splitting the string you get from button.getName() around the space. It is not an especially efficient method, but it sounds a little like what you are (or were, by now) looking for.

提交回复
热议问题