所以我想做的是单击一次后保持 mousePressed 方法“打开”,即使我自己没有按住它。然后再次单击后,它将“关闭”
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// save coord x,y when mouse is pressed
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// coord x,y when drag mouse
currentX = e.getX();
currentY = e.getY();
if (g2 != null) {
// draw oval if g2 context not null
g2.drawOval(oldX, oldY, width, height);
g2.fillOval(oldX, oldY, width, height);
// refresh draw area to repaint
repaint();
// store current coords x,y as olds x,y
oldX = currentX;
oldY = currentY;
}
}
});
最佳答案
处理
mousePressed()
事件。然后在您的类中保留一个变量(假设为“clickCounter”),每次生成事件时都会递增该变量。然后您需要处理
mouseMoved()
事件以了解鼠标何时移动。
现在,mouseMoved()
事件中的逻辑可以检查变量是否为奇数,这表明鼠标刚刚被单击:
if (clickCounter % 2 == 1)
{
add your logic here
}
或者您可以保留一个 boolean 变量,每次单击鼠标时都会关闭/打开该变量。
关于java - 单击后保持 mousePressed 处于 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33555650/