鼠标按下事件被触发两次

时间:2015-04-10 02:39:02

标签: java mouseevent mouselistener mousepress

我试图拥有它,所以一旦按下鼠标,它就会被触发一次而且只被触发一次。然而,它一直被解雇两次。我不确定这是因为我正在通过for循环,但我希望鼠标监听器能够捕获我点击的数组的索引。这就是为什么我将它分配给2d数组中的每个索引,我不确定是否有更好的方法。

代码:

public boolean firstClick = false;
public boolean secondClick = false;
for (int i = 7; i >= 0; i--) {
        for (int j = 0; j < 8; j++) {
            int tempi = i;
            int tempj = j;
            pnlCells[i][j].add(getPieceObject(str[(7 - i)][j]), BorderLayout.CENTER);
            pnlCells[i][j].validate();
            pnlCells[i][j].addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {

                    try {
                        if (firstClick == false || secondClick == false) {


                                if (firstClick == false) {
                                    mouseX = tempj;
                                    mouseY = 7 - tempi;
                                     System.out.println("First You pressed" +  mouseX + ", " + mouseY);   
                                    firstClick = true;
                                    sourceColor = pnlCells[mouseX][mouseY].getForeground();
                                    pnlCells[mouseX][mouseY].setForeground(Color.yellow);
                                    pnlCells[mouseX][mouseY].repaint();
                                    pnlBoard.repaint();
                                    pnlMain.repaint();
                                } else if (secondClick == false) {
                                    newMouseX = tempj;
                                    newMouseY = 7 - tempi;
                                    System.out.println("Second You pressed" +  newMouseX + ", " + newMouseY);  
                                    secondClick = true;
                                }

                                if (firstClick == true && secondClick == true) {
                                    firstClick = false;
                                    secondClick = false;
                                    pnlCells[mouseX][mouseY].setForeground(sourceColor);
                                    pnlCells[mouseX][mouseY].repaint();
                                    pnlBoard.repaint();
                                    pnlMain.repaint();
                                    PlayerMove pM = turn(); //send turn to server
                                    objectOut.writeObject(pM); //send turn to server
                                    System.out.println(name + ": sent move to server");
                                    s.suspend();
                                }


                        }
                    } catch (IOException ex) {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            //System.out.println(j+", "+(i)+":"+v.str[(7-i)][j]);

        }
    }

一旦我点击鼠标,它就会一直打印出来 首先你按了xcoord1,ycoord1 第二个你按xcoord2,ycoord2

1 个答案:

答案 0 :(得分:0)

我假设init的firstClick和secondClick都为false。然后当我们进入循环时,firstClick将始终触发,然后将其设置为true。在下一次迭代中,secondClick将始终触发,因为它是false并且firstClick不会触发,因为它现在是真的。

将其中任何一个触发后,将firstClick和secondClick都设置为true。