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

	}

}

Java程序设计——鼠标监视器_第1张图片

 

你可能感兴趣的:(java,计算机外设,python)