代码运行良好。当我左键单击时,它会执行代码描述的所有操作。唯一的问题是我不想单击同一个位置!我找不到解决方案。有什么建议吗?
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
innerCells[i][j] = new JPanel();
innerCells[i][j].setLayout(new BorderLayout());
innerCells[i][j].setBorder(BorderFactory.createLineBorder(lineColor));
innerCells[i][j].setBackground(backgroundColor);
innerCells[i][j].addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JPanel k = (JPanel) e.getSource();
JLabel l = new JLabel("", JLabel.CENTER);
int x = getRows();
int y = getCols();
for (int r = 0; r < getRows(); r++) {
for (int c = 0; c < getCols(); c++) {
if (innerCells[r][c] == k) {
x = r;
y = c;
}
}
}
if (array[x][y] == 0) {
l.setBackground(k.getBackground());
k.add(l);
k.setBackground(Color.white);
k.revalidate();
} else {
l.setBackground(k.getBackground());
k.add(l);
k.setBackground(Color.red);
k.revalidate();
}
randomHits();
}