码迷,mamicode.com
首页 > 其他好文 > 详细

TrackEvent 鼠标键盘各种事件

时间:2014-11-15 15:36:48      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:java   事件 swing   

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 鼠标键盘各种事件

标签:java   事件 swing   

原文地址:http://blog.csdn.net/xqw8922/article/details/41145921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!