首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图层中的Java面板..鼠标事件未流过

图层中的Java面板..鼠标事件未流过
EN

Stack Overflow用户
提问于 2015-05-06 04:23:45
回答 2查看 48关注 0票数 2

我正在做connect 4程序,我遇到了鼠标事件的问题。这是UI的架构。

代码语言:javascript
复制
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);


            }
}

现在单元格定义为

代码语言:javascript
复制
public class Cell extends JPanel implements  MouseListener{
}

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

EN

回答 2

Stack Overflow用户

发布于 2015-05-06 04:28:12

您没有将gridPanel作为MouseListener添加到单元格中

代码语言:javascript
复制
        for (int j = 0; j < 7; j++) {
            Cell tempCell = new Cell(i,j);
            gridUI[i][j] = tempCell;
            gridPanel.add(tempCell);
            tempCell.addMouseListener(this);
        }
票数 1
EN

Stack Overflow用户

发布于 2015-05-06 04:31:34

找到我的问题了。在循环中添加actionlistner。

代码语言:javascript
复制
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);


            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30062583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档