The following demonstrates how to add a MouseListener to a Panel. You could add a MouseListener directly to a JFrame if you desired
```java
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
/**
*
* @author AJB
*/
public class MyPanel extends JPanel implements MouseListener{
int mouseX=0;
int mouseY=0;
MyPanel()
{
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Mouse clicked at " + mouseX + ", " + mouseY, 10, 20);
}
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
```