博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于鼠标移动监听接口:MouseMotionListener
阅读量:4257 次
发布时间:2019-05-26

本文共 1790 字,大约阅读时间需要 5 分钟。

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。

MouseMotionListener 用于接收组件上的鼠标移动事件的侦听器接口。旨在处理鼠标移动事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象类 MouseMotionAdapter(仅重写有用的方法)。

然后使用组件的addMouseMotionListener 方法将从该类所创建的侦听器对象向该组件注册。移动或拖动鼠标时会生成鼠标移动事件。(将生成很多此类事件)。发生鼠标移动事件时,将调用该侦听器对象中的相应方法,并将MouseEvent 传递给该方法。

此接口包括两个方法:

1.public void mouseDragged(MouseEvent e)

  鼠标按键在组件上按下并拖动时调用。(处理鼠标拖动事件)

2.public void mouseMoved(MouseEvent e)

鼠标光标移动到组件上但无按键按下时调用。(处理鼠标移动事件)

下面看一个直接实现MouseMotionListener接口的例子:鼠标移动或拖动时,给出提示并显示鼠标所在位置的坐标。

package cn;import java.awt.*;import java.awt.event.*;import javax.swing.JFrame;import javax.swing.JButton;public class MyMouseMotionListener implements MouseMotionListener {	JFrame myframe; // JFrame通常默认使用BorderLayout布局管理器的	TextArea tf;	JButton exitButton;	int number = 1;	public MyMouseMotionListener() {		Label label = new Label("click and drag the mouse");		myframe = new JFrame("MyMouseMotionListener");		tf = new TextArea();		exitButton = new JButton("退出");		tf.addMouseMotionListener(this);		exitButton.addActionListener(new ActionListener() {			@Override			public void actionPerformed(ActionEvent e) {				System.exit(0);			}		});		myframe.add(label, BorderLayout.NORTH);		myframe.add(tf, BorderLayout.CENTER);		myframe.add(exitButton, BorderLayout.SOUTH);		myframe.setSize(400, 300);		myframe.setVisible(true);	}	public static void main(String[] args) {		new MyMouseMotionListener();	}	@Override	// 负责处理鼠标拖动事件	public void mouseDragged(MouseEvent e) {		//getX(),getY():获取鼠标的坐标位置		String s = number++ + "" + "the mouse is draggered:x=" + e.getX()				+ "y=" + e.getY() + "\n";		tf.append(s);	}	@Override	// 负责处理鼠标移动事件	public void mouseMoved(MouseEvent e) {		String s = number++ + "" + "the mouse is moving:x=" + e.getX() + "y="				+ e.getY() + "\n";		tf.append(s);	}}

 

转载地址:http://vvtei.baihongyu.com/

你可能感兴趣的文章
MFCC(Mel 倒谱系数)
查看>>
python2代码批量转为python3代码
查看>>
贝叶斯优化: 一种更好的超参数调优方式
查看>>
Tensorflow 多任务学习 概念介绍
查看>>
Keras 多任务实现,Multi Loss #########Keras Xception Multi loss 细粒度图像分类
查看>>
#####好好好####从Google Visor到Microsoft NNI再到Advisor调参服务接口发展史
查看>>
tensorflow中的共享变量(sharing variables) 最佳方式variable_scope()命名空间来完成
查看>>
深度增强学习综述
查看>>
###好好好####Tensorflow将模型导出为一个文件及接口设置
查看>>
简明条件随机场CRF介绍(附带纯Keras实现)
查看>>
####好好好#######让Keras更酷一些!Keras模型杂谈
查看>>
##############缺失值填充的几种方法
查看>>
Keras 处理 不平衡的数据的分类问题 imbalance data 或者 highly skewed data
查看>>
#####好好好好####Keras深度神经网络训练分类模型的四种方法
查看>>
[NLP自然语言处理]谷歌BERT模型深度解析
查看>>
######好好好######MSE与CE的区别?数学推导 本质理解
查看>>
######好好好,本质#####基于LSTM搭建一个文本情感分类的深度学习模型:准确率往往有95%以上
查看>>
深度学习最全优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)
查看>>
训练loss不下降原因集合
查看>>
通过GAN网络生成样本的一些想法创造数据
查看>>