我很好奇为什么单击鼠标时得到的结果是“正确的”但错误的数目。
每次单击鼠标,我都应该在控制台mouseClicked上进行打印。但是我每次点击鼠标时都会打印出很多...有时是5点

mouseClicked
mouseClicked
mouseClicked
mouseClicked
mouseClicked


不只是

mouseClicked


为什么?

public class GUI extends JFrame implements MouseListener, ActionListener {
.....
   public GUI {
      GridBagLayout m = new GridBagLayout();
      Container c = getContentPane();
      c.setLayout (m);
      GridBagConstraints con = new GridBagConstraints();
      ....
      JPanel pDraw = new JPanel(new GridLayout(ROWS,COLS,2,2));
      con = new GridBagConstraints();
      ...
      m.setConstraints(pDraw, con);

      pDraw.addMouseListener(this);
      pack();
      setVisible(true);
   }

   public void mouseClicked(MouseEvent arg0) {
      System.out.println("mouseClicked");
   }
}

最佳答案

看看MouseEvent中的此方法:

getClickCount

公共诠释getClickCount()
返回与此事件关联的鼠标单击次数。
返回值:
点击次数的整数值

09-25 20:39