MouseListener on JFrame never being called
I'm trying to use a MouseListener to see when something is clicked in my JFrame, but none of the events (mouseClicked, mouseEntered, etc) are ever called. I'm using a Canvas to draw the game onto the JFrame and I'm wondering if that could be the issue, but I'm not sure how to fix it. I was following this tutorial and my code looks almost exactly the same.
Here's my JFrame class:
public class Display extends JFrame implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JFrame frame;
private MyCanvas canvas;
public Display (int r, int c, Card[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
frame.setLayout(null);
frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new MyCanvas(b);
frame.add(canvas);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
The primary thing you were doing wrong was to extend JFrame and also create an explicit instance of one. Do it as follows.
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Display implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JPanel canvas;
private JFrame frame;
public static void main(String[] args) {
new Display(3,3,new int[3][3]);
}
public Display (int r, int c, int[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
// frame.setLayout(null);
// frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new JPanel();
canvas.setPreferredSize(new Dimension(width,height));
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
Points to consider
- don't extend JFrame, just use an instance.
- Canvas is old. I recommend using a JPanel to hold the components and/or painting
-
frame.setLocationRelativeTo(null)
centers your frame on the screen. - Since I don't have the
Card
class I usedint[][]
to demonstrate this. - Anything else I did, check out the API documentation for explanations.
For more on GUI's, etc, check out the Java Tutorials