0

我正在尝试拥有它,因此一旦按下鼠标,它就会被触发一次,并且只有一次。但是,它不断被解雇两次。我不确定这是否是因为我正在经历一个 for 循环,但我希望鼠标侦听器捕捉我单击的数组的索引。这就是为什么我将它分配给二维数组中的每个索引,我不确定是否有更好的方法来做到这一点。

代码:

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]);

        }
    }

一旦我单击鼠标,它总是会打印 First You press xcoord1, ycoord1 Second You press xcoord2, ycoord2

4

1 回答 1

0

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

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

于 2015-04-10T02:48:24.693 回答