层中的Java面板。。鼠标事件未通过

层中的Java面板。。鼠标事件未通过,java,mouselistener,Java,Mouselistener,我正在开发connect 4程序,我在鼠标事件方面遇到了问题。下面是UI的体系结构 public class PlayConnect implements MouseListener { mainFrame = new JFrame("Connect-4"); basePanel = new JPanel(); mainFrame.add(basePanel); gridPanel = new JPanel(); gridPanel.addMouseListener(this); gridP

我正在开发connect 4程序,我在鼠标事件方面遇到了问题。下面是UI的体系结构

public class PlayConnect implements  MouseListener {

mainFrame = new JFrame("Connect-4");
basePanel = new JPanel();
mainFrame.add(basePanel);
gridPanel = new JPanel();
gridPanel.addMouseListener(this);
gridPanel.setLayout(new GridLayout(6, 7));
   for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                Cell tempCell = new Cell(i,j);
                gridUI[i][j] = tempCell;
                gridPanel.add(tempCell);


            }
}

单击单元格时,会调用单元格的MouseClicked方法,但对于类PlayConnect,则不会调用该方法。我不知道为什么。我确实尝试将gridPanel的类型更改为JLayeredPane,但也没有任何帮助

您没有将gridPanel作为鼠标侦听器添加到单元格中

        for (int j = 0; j < 7; j++) {
            Cell tempCell = new Cell(i,j);
            gridUI[i][j] = tempCell;
            gridPanel.add(tempCell);
            tempCell.addMouseListener(this);
        }
for(int j=0;j<7;j++){
单元tempCell=新单元(i,j);
gridUI[i][j]=tempCell;
添加(tempCell);
tempCell.addMouseListener(这个);
}

发现了我的问题。在循环中添加了actionlistner

for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                Cell tempCell = new Cell(i,j);
                tempCell.addMouseListner(this); // New listener
                gridUI[i][j] = tempCell;
                gridPanel.add(tempCell);


            }
for(int i=0;i<6;i++){
对于(int j=0;j<7;j++){
单元tempCell=新单元(i,j);
tempCell.addMouseListner(this);//新建侦听器
gridUI[i][j]=tempCell;
添加(tempCell);
}

哇,你太快了!最小化Chrome窗口后,我就看到了问题!
for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                Cell tempCell = new Cell(i,j);
                tempCell.addMouseListner(this); // New listener
                gridUI[i][j] = tempCell;
                gridPanel.add(tempCell);


            }