I'm new in Java.
I'm making Mines. I'm using GridLayout with paramteres (x_length, y_length). I'd like know which button I pressed -> his coordinates (x,y). But if I type it to listener it gives me error -> change modifier of 'x' to final. So my ask is how can I simply get coordinates of buttons?
And I also want to ask how can I simply change size of buttons? setSize does not work for me.
for (int y = 0; y < y_length; y++)
{
for (int x = 0; x < x_length; x++)
{
buttons[x][y] = new JButton("X");
buttons[x][y].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON1)
{
//exception -> Cannot refer to a non-final variable x inside an inner class defined in a different method
JOptionPane.showMessageDialog(null, "Left -> " + x + " | " + y);
}
else if (e.getButton() == MouseEvent.BUTTON3)
{
JOptionPane.showMessageDialog(null, "Right -> " + x + " | " + y);
}
}
});
mines_array.add(buttons[x][y]);
}
}