Java AWT 鼠标事件
我有一个使用 AWT 事件模型的小程序。它有一个布尔值,表示左按钮是否被按下。这是一个示例代码:
public class Game extends Applet implements MouseListener
{
boolean isLeftButtonPressed;
public void init()
{
addMouseListener(this);
isLeftButtonPressed = false;
}
public void paint(Graphics g)
{
g.drawString("Is Button Pressed: " + isLeftButtonPressed, 20, 20);
}
@Override
public void mouseClicked(MouseEvent e)
{
isLeftButtonPressed = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e)
{
isLeftButtonPressed = false;
repaint();
}
//Other MouseListener methods not listed but have to be implemented
}
但似乎左按钮永远不会释放,即使在您实际这样做之后也是如此。可能是什么问题?
I have an applet that makes use of the AWT event model. It has a boolean that says if the left button is pressed or not. Here is a sample code:
public class Game extends Applet implements MouseListener
{
boolean isLeftButtonPressed;
public void init()
{
addMouseListener(this);
isLeftButtonPressed = false;
}
public void paint(Graphics g)
{
g.drawString("Is Button Pressed: " + isLeftButtonPressed, 20, 20);
}
@Override
public void mouseClicked(MouseEvent e)
{
isLeftButtonPressed = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e)
{
isLeftButtonPressed = false;
repaint();
}
//Other MouseListener methods not listed but have to be implemented
}
But it seems as if the left button is never released, even after you actually do so. What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这其中的根本是不正确的,
这些是鼠标事件,
因此,当您处理单击事件时,意味着鼠标被单击并释放。
所以我认为你必须使用鼠标按下而不是单击。
MouseEvent
The fundamental in this is incorrect,
These are the mouse events,
So, when you handle the click event that means mouse is clicked and released.
So i think you have to use mousepressed instead of clicked.
MouseEvent
mouseClicked
方法将在mouseReleased
方法之后调用,因此 isLgetButtonPressed 的值为 true。您必须使用MouseEvent.getButton()
方法来检查按下了哪个鼠标按钮。Method
mouseClicked
will be called aftermouseReleased
method so value of isLgetButtonPressed will be true. You have to useMouseEvent.getButton()
method to check which mouse button is pressed.