gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:04:21 26 4
gpt4 key购买 nike

我正在开发 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);


}
}

现在 Cell 定义为

public class Cell extends JPanel implements  MouseListener{
}

Cell 的 MouseClicked 方法在单击单元格时被调用,但不是为类 PlayConnect 调用。我不确定为什么。我确实尝试将 gridPanel 的类型更改为 JLayeredPane,但它也没有帮助。

最佳答案

您没有将 gridPanel 作为 MouseListener 添加到您的 Cells

        for (int j = 0; j < 7; j++) {
Cell tempCell = new Cell(i,j);
gridUI[i][j] = tempCell;
gridPanel.add(tempCell);
tempCell.addMouseListener(this);
}

关于层中的 Java 面板 .. 鼠标事件未流过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com