You can create a class that implements
the MouseListener
interface
as so:
public class CustomMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: ("+e.getX()+", "+e.getY() +")");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
You want define each of those methods based on the action the method represents (which are self explanatory). The MouseEvent
object will have all the info you need related to the mouse (ex. x and y position of mouse).
Now you want to add this new MouseListener
to a JPanel
(which in this case is your Board
class):
//JPanel panel = new JPanel();
Board panel = new Board();
panel.addMouseListener(new CustomMouseListener());