So here is the entire file I am using, and in desperation, I put the x and y to be defined in every event but I still get nothing.. this is a multi-class project, where should I do addMouseListener()? and why isnt this picking up the event? (note: the printing of the coordinates in run() works.) This is an applet project and the applet works fine just doesnt register this event.
public class ClickCheck implements Runnable, java.awt.event.MouseListener {
public int x, y;
public ClickCheck() {
Thread t = new Thread(this);
t.start();
}
public void mouseClicked(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mousePressed(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseReleased(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseEntered(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseExited(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void run() {
System.out.println(x + "," + y);
}
}
Assuming you want to add a
MouseListenerto the main window/panel area of your applet you would add this to your appletinit()method:Also it’s very unusual to have
Threadincluded in yourMouseListener. The thread itself will print out co-ordinates & exit immediately.