Java程序设计——鼠标监视器

复制代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 例98
{
	public void designFrame() {
		JFrame mywindow=new JFrame("MouseDemo");
		Container container=mywindow.getContentPane();
		mywindow.setVisible(true);
		mywindow.setBounds(0,0,300,100);
		container.addMouseListener(new MouseHandler());
		container.addMouseMotionListener(new MouseHandler());
		mywindow.setDefaultCloseOperation(mywindow.EXIT_ON_CLOSE);
		
	}
	class MouseHandler implements MouseListener,MouseMotionListener{
		public void mouseClicked(MouseEvent e) {
			int x=e.getX();
			int y=e.getY();
			System.out.println("鼠标点击的位置X:"+x+"\ty:"+y);
		}
		public void mousePressed(MouseEvent e) {
			int i=e.getButton();
			if(i==MouseEvent.BUTTON1);
				System.out.println("按下鼠标左键");
			if(i==MouseEvent.BUTTON2);
				System.out.println("按下鼠标中建");
			if(i==MouseEvent.BUTTON3);
				System.out.println("按下鼠标右键");
		}
		public void mouseReleased(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {
			System.out.println("鼠标移出窗口");
		}
		public void mouseDragged(MouseEvent e) {
			System.out.println("鼠标在窗体上拖曳");
		}
		public void mouseMoved(MouseEvent e) {
			System.out.println("鼠标在窗体上移动");
		}
}
	public static void main(String[] args)
	{
		例98 myframe=new 例98();
		myframe.designFrame();// TODO Auto-generated method stub

	}

}
相关推荐
Csvn16 分钟前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
nanxun88616 小时前
记一次诡异的 Docker 容器"串包"故障排查
java
金銀銅鐵17 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
用户15630681035119 小时前
Day01 | Java 基础(Java SE)
java
行者全栈架构师21 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
cup1121 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
行者全栈架构师1 天前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_01 天前
mac(m5)平台编译openjdk
java