首页 > 代码库 > TrackEvent 鼠标键盘各种事件

TrackEvent 鼠标键盘各种事件

package com.wxq.learning;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TrackEvent extends JFrame
{
	private HashMap<String, JTextField> h = new HashMap<String, JTextField>();
	private String[] event =
	{ "focusGained", "focusLost", "keyPressed", "keyReleased", "keyTyped", "mouseClicked", "mouseEntered", "mouseExited", "mousePressed", "mouseReleased",
			"mouseDragged", "mouseMoved" };
	private MyButton b1 = new MyButton(Color.BLUE, "test1"), b2 = new MyButton(Color.RED, "test2");

	class MyButton extends JButton
	{
		void report(String field, String msg)
		{
			h.get(field).setText(msg);
		}

		FocusListener fl = new FocusListener()
		{

			@Override
			public void focusGained(FocusEvent e)
			{
				report("focusGained", e.toString());
			}

			@Override
			public void focusLost(FocusEvent e)
			{
				report("focusLost", e.toString());
			}

		};
		KeyListener kl = new KeyListener()
		{

			@Override
			public void keyTyped(KeyEvent e)
			{
				report("keyTyped", e.toString());
			}

			@Override
			public void keyPressed(KeyEvent e)
			{
				report("keyPressed", e.toString());
			}

			@Override
			public void keyReleased(KeyEvent e)
			{
				report("keyReleased", e.toString());

			}

		};
		MouseListener ml = new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent e)
			{
				report("mouseClicked", e.toString());
			}

			@Override
			public void mousePressed(MouseEvent e)
			{
				report("mousePressed", e.toString());
			}

			@Override
			public void mouseReleased(MouseEvent e)
			{
				report("mouseReleased", e.toString());
			}

			@Override
			public void mouseEntered(MouseEvent e)
			{
				report("mouseEntered", e.toString());
			}

			@Override
			public void mouseExited(MouseEvent e)
			{
				report("mouseExited", e.toString());

			}

		};
		MouseMotionListener mml = new MouseMotionListener()
		{

			@Override
			public void mouseDragged(MouseEvent e)
			{
				report("mouseDragged", e.toString());
			}

			@Override
			public void mouseMoved(MouseEvent e)
			{
				report("mouseMoved", e.toString());
			}

		};

		public MyButton(Color color, String label)
		{
			super(label);
			setBackground(color);
			addFocusListener(fl);
			addKeyListener(kl);
			addMouseListener(ml);
			addMouseMotionListener(mml);
		}
	}

	public TrackEvent()
	{
		setLayout(new GridLayout(event.length + 1, 2));
		for (String e : event)
		{
			JTextField t = new JTextField();
			t.setEditable(false);
			add(new JLabel(e, JLabel.RIGHT));
			add(t);
			h.put(e, t);
		}
		add(b1);
		add(b2);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		SwingConsole.run(new TrackEvent(), 500, 400);
	}

}

TrackEvent 鼠标键盘各种事件