mouseevt.java

来自「java练习程序」· Java 代码 · 共 72 行

JAVA
72
字号
//例子处理了5种鼠标事件
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MouseEVT extends Applet implements MouseListener
{
	TextField text;int x,y,tom;
	public void init()
	{
		x=5; tom=0; text=new TextField(40);
		add(text);
		addMouseListener(this);//向小程序增加监视器
	}
	public void paint(Graphics g)
	{
		if(tom==1)
		{
			g.setColor(Color.red);
			g.fillOval(10+(int)x,10+(int)y,15,15);
			g.setColor(Color.green); g.fillOval(100,100,30,30);
			g.setColor(Color.red); g.fillOval(120,120,35,35);
		}
		else if(tom==-1)
		{
			g.setColor(Color.pink); g.fillOval(100,100,50,50);
			g.setColor(Color.white); g.fillOval(120,120,45,45);
		}
		else if(tom==2)
		{
			g.setColor(Color.red);
			g.drawString("来了,漫画带来了吗?",80,120);
		}
		else if(tom==-2)
		{
			g.setColor(Color.green);
			g.drawString("欢迎再来,别忘了关门",80,120);
		}
	}
	
	public void mousePressed(MouseEvent e)
	{
		tom=1; x=e.getX(); y=e.getY();
		text.setText("鼠标键按下了,位置是"+e.getX()+","+e.getY());
		repaint();
	}
	
	public void mouseReleased(MouseEvent e)
	{
		tom=-1;text.setText("鼠标松开了;位置是"+e.getX()+","+e.getY());
		repaint();
	}
	public void mouseEntered(MouseEvent e)
	{
		tom=2; text.setText("鼠标进来了,位置是"+e.getX()+","+e.getY());
		repaint();
	}
	
	public void mouseExited(MouseEvent e)
	{
		tom=-2; text.setText("鼠标走开了");
		repaint();
	}
	public void mouseClicked(MouseEvent e)
	{
		if(e.getClickCount()==2)
		{
			text.setText("鼠标键双击,位置:"+e.getX()+","+e.getY());
		}
	}
}	

⌨️ 快捷键说明

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