首页 > 代码库 > 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 鼠标键盘各种事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。