⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventhandlingdemo3.java

📁 JAVA程序设计课程中各章节的程序实例。
💻 JAVA
字号:
/**
*A simple mouse event handling demonstration program
*2004.12.28. xhcprince
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class EventHandlingDemo3 extends Applet
{
	String str;
	Point previous,current;
	
	public void init()
	{
		str = "Initialized";
		resize(300,300);
		
		addMouseMotionListener(new MouseMotionHandler());
		addMouseListener(new MouseHandler());
		
		previous = new Point(0,0);
		current = new Point(0,0);
		showStatus("Initializing");
	}

	public void paint(Graphics g)
	{
		showStatus("Applet painted");
	}

	class MouseHandler implements MouseListener	//Inner class
	{
	/*-------------------------------------------------------------------------
		The MouseListener interface is for the functionality of the mouse
		buttons,if we want to trap the events generated at the click or release
		of a mouse button then we implements the MouseListener interface
	-------------------------------------------------------------------------*/
		public void mousePressed(MouseEvent e)	//you have to implement all the methods!
		{
			previous.x = current.x = e.getX();
			previous.y = current.y = e.getY();
		}
	
		public void mouseClicked(MouseEvent e){}
		
		public void mouseReleased(MouseEvent e){}
		
		public void mouseEntered(MouseEvent e){}
		
		public void mouseExited(MouseEvent e){}
	}

	class MouseMotionHandler implements MouseMotionListener
	{
	/*-------------------------------------------------------------------------
		The MouseMotionListener interface is for the mouse movement events.This
		is usually implemented if we want to trap the movement of the mouse on 
		the application window or the applet
	-------------------------------------------------------------------------*/
		public void mouseDragged(MouseEvent e)	//you have to implement all the methods!
		{
			str = "Mouse dragged";
			showStatus(str);
			
			current.x = e.getX();
			current.y = e.getY();
			
			Graphics g = getGraphics();
			g.setColor(Color.red);
			g.drawLine(previous.x,previous.y,current.x,current.y);
			
			previous.x = current.x;
			previous.y = current.y;
		}
	
		public void mouseMoved(MouseEvent e)
		{
			str += " Mouse moved";
			showStatus(str);
		}
	}
}
/*
<applet code = "EventHandlingDemo3.class" width = 800 height = 400>
</applet>
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -