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

后端 未结 5 2108
不知归路
不知归路 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条回答
  •  遥遥无期
    2020-11-22 01:44

    You have saved an array of all JButtons; you could search for ae.getSource() and you have the position.

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if( b[i][j] == ae.getSource() ) { 
          // position i,j
        }
      }
    }
    

提交回复
热议问题