java小程序中鼠标事件的响应处理(包含右键、双击等)

javaduke 原创于 www.dukejava.com 主页上提供大量java编程技巧
如何在小程序中实现鼠标响应?
public class Hello extends Applet implements MouseListener{

public void init(){
addMouseListener(this);
}
public void mouseEntered(MouseEvent e){
au.play();
setBackground(Color.blue);
}

public void mouseExited(MouseEvent e){
setBackground(Color.black);
}

public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

}
首先在小程序中实现事件监听器,再实现鼠标用户接口


如何小程序中处理鼠标双、三击?

component.addMouseListener(
new MyMouseListener());

public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 3) {
// 处理鼠标三击
} else if (evt.getClickCount() == 2)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
PyQt可以使用以下三个方法来处理鼠标左键,键和右键事件: 1. `mousePressEvent(event)`:当用户按下鼠标按钮时调用此方法,可以通过 `event.button()` 方法来判断鼠标的哪个按钮被按下。 2. `mouseReleaseEvent(event)`:当用户释放鼠标按钮时调用此方法,可以通过 `event.button()` 方法来判断鼠标的哪个按钮被释放。 3. `mouseDoubleClickEvent(event)`:当用户双击鼠标按钮时调用此方法,可以通过 `event.button()` 方法来判断鼠标的哪个按钮被双击。 下面是一个简单的示例程序,演示如何处理鼠标左键,键和右键事件: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget class MouseDemo(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Mouse Demo') self.show() def mousePressEvent(self, event): if event.button() == 1: print('Left button pressed') elif event.button() == 2: print('Middle button pressed') elif event.button() == 4: print('Right button pressed') def mouseReleaseEvent(self, event): if event.button() == 1: print('Left button released') elif event.button() == 2: print('Middle button released') elif event.button() == 4: print('Right button released') def mouseDoubleClickEvent(self, event): if event.button() == 1: print('Left button double clicked') elif event.button() == 2: print('Middle button double clicked') elif event.button() == 4: print('Right button double clicked') if __name__ == '__main__': app = QApplication(sys.argv) ex = MouseDemo() sys.exit(app.exec_()) ``` 在这个示例程序,我们创建了一个 `MouseDemo` 类,继承自 `QWidget` 类。在 `initUI()` 方法设置了窗口的初始大小和标题,并且显示出来。 然后我们重载了三个方法来处理鼠标左键,键和右键事件。在 `mousePressEvent()` 方法,我们使用 `event.button()` 方法来判断鼠标的哪个按钮被按下。在 `mouseReleaseEvent()` 方法,我们使用 `event.button()` 方法来判断鼠标的哪个按钮被释放。在 `mouseDoubleClickEvent()` 方法,我们使用 `event.button()` 方法来判断鼠标的哪个按钮被双击。 最后,我们使用 `QApplication` 类创建了一个应用程序对象,并将 `MouseDemo` 类的实例作为主窗口。最后调用 `sys.exit(app.exec_())` 来运行应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaduke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值