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
.
答案 0 :(得分:0)
您正在创建一个Handler对象,很好,您将其添加到Canvas对象(this
-为什么选择Canvas?),并且正在创建一个“顶级”窗口对象,该对象实际上是“窗口”类型,但是您永远不会将“画布”添加到窗口中,也不会显示“窗口”,因此没有理由指望此代码实际上可以工作。
现在,我猜想那里还有更多代码没有向我们展示,这可能具有相关性,如果是这样,请考虑创建并发布适当的MCVE以便为我们提供最好地了解您的问题。
好的,我用您的代码创建了一个MCVE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends Canvas {
private static final long serialVersionUID = 1673528055664762143L;
private static final int WIDTH = 416, HEIGHT = 439;
public Main() {
Procode238Window window = new Procode238Window(WIDTH, HEIGHT, "DARRAGH", this);
this.addMouseListener(new ClickEvent());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Main();
});
}
}
class ClickEvent extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("hello");
}
}
// renamed to avoid clashing with the java.awt.Window class
class Procode238Window extends Canvas {
private static final long serialVersionUID = 6733885629776844621L;
public Procode238Window(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(); // this method doesn't exist
}
}
它有效
请注意:
java.awt.Window
类的混淆.start()
方法,该方法未显示。这会引起问题吗?