I'm making a chess-like game in java and I'm having an issue with the click events. The mouseClicked
function isn't responding to my clicks on the window and for no apparent reason.
I have already tried a few things such as changing class names and using different functions but nothing has worked.
package main.game.com;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ClickEvent extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("hello");
}
}
package main.game.com;
import java.awt.Canvas;
public class Main extends Canvas {
private static final long serialVersionUID = 1673528055664762143L;
private static final int WIDTH = 416, HEIGHT = 439;
public Main() {
Window window = new Window(WIDTH, HEIGHT, "DARRAGH", this);
this.addMouseListener(new ClickEvent());
}
package main.game.com;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Window extends Canvas {
private static final long serialVersionUID = 6733885629776844621L;
public Window(int width, int height, String title, Main main) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(main);
frame.setVisible(true);
main.start();
}
}
The first set of code is my mouseAdapter
library and the second is the first part of my main class containing the clickListener
.