利用Java事件处理机制实现录制、回放功能
http://www.ibm.com/developerworks/cn/java/j-java-test/
(1) Put event in system event queue:
import java.awt.EventQueue;
import java.awt.*;
import java.util.*;
public class MyQueueEvent extends EventQueue {//??EventQueue???
public MyQueueEvent() {
}
public static void main(String[] args) {
//impleExample.main(new String[]{null}) ;
MyQueueEvent myQueueEvent1 = new MyQueueEvent();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(myQueueEvent1) ;
}
public void dispatchEvent(AWTEvent ae){
if(ae.getSource() instanceof javax.swing.JButton)
System.out.println("My apture:"+((javax.swing.JButton)ae.getSource()).getText()) ;
super.dispatchEvent(ae);
}
}
(2) play back: using Robot
mport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestReplay extends Thread {
public static void main(String[] args) {
try {
Frame1.main(new String[] { null });
Thread.currentThread().sleep(3000);
Robot robottest = new Robot();
robottest.waitForIdle();
Frame jframe = getFrame("test");
;
JButton jbtn = getButton(jframe, "jButton1");
robottest.mouseMove(jbtn.getLocationOnScreen().x + jbtn.getWidth()
/ 2, jbtn.getLocationOnScreen().y + jbtn.getHeight() / 2);
robottest.mousePress(InputEvent.BUTTON1_MASK);
robottest.mouseRelease(InputEvent.BUTTON1_MASK);
jbtn.setText("aaa");
// jbtn.getActionListeners()
} catch (Exception ee) {
ee.printStackTrace();
}
}
private static Frame getFrame(String title) {
Frame[] jframes = (Frame[]) JFrame.getFrames();
for (int i = 0; i < jframes.length; i++) {
if (jframes[i].getTitle().equalsIgnoreCase(title))
return jframes[i];
}
return null;
}
private static JButton getButton(Frame jf, String text) {
Component[] coms = ((JFrame) jf).getContentPane().getComponents();
for (int i = 0; i < coms.length; i++) {
if (!(coms[i] instanceof JButton))
continue;
if (((JButton) coms[i]).getText().equalsIgnoreCase(text))
return (JButton) coms[i];
}
return null;
}
public void run() {
}
}