Hello all,
I'm making a simple game in minesweeper, and I have a Mine class with a JButton and some other information, namely an x and y variable for its location. In a class called Board, a constructor creates a multi-dimensional array of Mine objects. I tried adding mouselistener to the constructor of buttons but it did not work, only registering the last object of the array. When I add the MouseListener to the Board constructor, I cannot refer to the x and y variables inside the action performed in MouseListener, as shown below:
int yCounter = 0;
int xCounter = 0;
for(xCounter = 0;xCounter < 10;xCounter++){
for(yCounter = 0;yCounter < 10; yCounter++){
array[xCounter][yCounter] = new Mine(xCounter, yCounter);
frame.add(array[xCounter][yCounter].getButton());
frame.add(array[xCounter][yCounter].getNumber());
array[xCounter][yCounter].getButton().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
if(e.getButton() == 3)
System.out.println("right click at" + array[xCounter][yCounter].getX() + array[xCounter][yCounter].getY());
}
});
}
yCounter = 0;
}
However I cannot refer to the xCounter and yCounter variables, but I need to get those variables in order for the actual game functions (not used at this time)
Any ideas on how i can add a listener and still get those x and y variables?
Any help is appreciated,
Phil