Simple ActionListener within a 2D array of JButtons

后端 未结 4 1448
不思量自难忘°
不思量自难忘° 2020-12-04 00:56

Okay so I am making a 2d array of JToggleButtons. I got the action listener up and going, but I have no way to tell which button is which.

If I click one, all it ret

4条回答
  •  悲哀的现实
    2020-12-04 01:42

    I made a minesweeper game and ran into a similar problem. One of the only ways you can do it, is to get the absolute location of the clicked button, then divide that by the x and y between buttons, so for me it was

    if ((e.getComponent().getX() != (randx) * 25 && e.getComponent().getY() != (randy) * 25) &&bomb[randx][randy] == false) {
    

    This code was to check if the area had bombs. So I had 25 x and y difference between location of bombs. That will just give you a general idea on how to do this.

    I believe: (x - x spacing on left side) / buffer - 1 would work.

提交回复
热议问题