Having Problem With Click Listener in Java

时间:2019-02-14 22:21:44

标签: java swing awt mouselistener

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.

1 个答案:

答案 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
    }
}

有效

请注意:

  • 可以将整个代码复制并粘贴到单个IDE文件中,然后运行并具有必要的main方法,这两个方法都必须成为MCVE
  • 我已经重命名了Window类,以避免命名冲突和与java.awt.Window类的混淆
  • 您的代码调用Main类的.start()方法,该方法未显示。这会引起问题吗?