📄 trackevent.java
字号:
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TrackEvent extends JFrame {
HashMap<String, JTextField> h = new HashMap<String, JTextField>();
String[] event = { "focusGained", "focusLost", "keyPressed", "keyReleased",
"keyTyped", "mouseClicked", "mouseEntered", "mouseExited",
"mousePressed", "mouseReleased", "mouseDragged", "mouseMoved" };
MyButton b1 = new MyButton(Color.blue, "test1");
MyButton b2 = new MyButton(Color.red, "test2");
class MyButton extends JButton {
void report(String field, String msg) {
((JTextField) h.get(field)).setText(msg);
}
FocusListener fl = new FocusListener() {
public void focusGained(FocusEvent e) {
report("focusGained", e.paramString());
}
public void focusLost(FocusEvent e) {
report("focusLost", e.paramString());
}
};
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
report("keyPressed", e.paramString());
}
public void keyReleased(KeyEvent e) {
report("keyReleased", e.paramString());
}
public void keyTyped(KeyEvent e) {
report("keyTyped", e.paramString());
}
};
MouseListener ml = new MouseListener() {
public void mouseClicked(MouseEvent e) {
report("mouseClicked", e.paramString());
}
public void mouseEntered(MouseEvent e) {
report("mouseEntered", e.paramString());
}
public void mouseExited(MouseEvent e) {
report("mouseExited", e.paramString());
}
public void mousePressed(MouseEvent e) {
report("mousePressed", e.paramString());
}
public void mouseReleased(MouseEvent e) {
report("mouseReleased", e.paramString());
}
};
MouseMotionListener mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
report("mouseDragged", e.paramString());
}
public void mouseMoved(MouseEvent e) {
report("mouseMoved", e.paramString());
}
};
public MyButton(Color color, String label) {
super(label);
setBackground(color);
addFocusListener(fl);
addKeyListener(kl);
addMouseListener(ml);
addMouseMotionListener(mml);
}
}
public TrackEvent(String str) {
super(str);
Container c = getContentPane();
c.setLayout(new GridLayout(event.length + 1, 2));
Font font = new Font("Arial Bold",Font.ITALIC,16);
for (int i = 0; i < event.length; i++) {
JTextField t = new JTextField();
t.setEditable(false);
t.setFont(font);
JLabel jl = new JLabel(event[i], JLabel.RIGHT);
jl.setFont(font);
c.add(jl);
c.add(t);
h.put(event[i], t);
}
c.add(b1);
c.add(b2);
setSize(1000, 500);
setLocation(200, 150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TrackEvent te = new TrackEvent("TrackEvent");
}
} // /:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -