Java程序启动另外一个UI程序(具有图形用户界面的程序),例如操作系统的calc.exe(计算器或别的Java打包的jar程序),然后人工对calc.exe进行操作,在该过程中,控制台Java程序把操作的事件记录到一个文件中,控制台结束录制,然后重新启动UI程序,读取文件中的事件对UI程序进行操作(该过程就是回放)。PS:可能用到的类(我不太清楚):EvenQueue,InvocationHandler,Robot也许还用到反射等知识,例如识别程序的控件的问题等。
解决方案 »
- 在bat里面怎么运行java应用呀。。?
- Linux下客户端调用服务器端的rmi出现拒绝连接,高手求助!!!!!
- 有没有人使用log4j1.3的.怎么把日志写到数据库啊
- 急!急!急!!偶笔记本的WINXP的用户密码忘了怎么办!?在线等待!
- 如何获取系统下cpu,内存,硬盘使用情况
- 菜鸟问题:请高手指点。
- 怎么用Jbuilder6运行一个Java控制台
- 高手看一下,下面这个要求要是用java怎么实现啊?
- 问一个问题?
- 清除ComBox中所有项的方法是CleanItem还是别的什么?
- 求一向Frame中添加计时器的完整代码
- 我想实现经过n年之后本息和是原来的金额的2倍,可是我的程序无法的到正确的结果,求指教!
用一个ArrayList保存事件(暂且不用理会写文件的细节)的实现:import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;import 框架.CalculatorFrame;import javax.swing.JFrame;public class EventTracer2 {
private InvocationHandler handler;
Scanner input=new Scanner(System.in);
ArrayList<ActionEvent> elist=new ArrayList<ActionEvent>(); public EventTracer2(){
handler=new InvocationHandler(){
public Object invoke(Object proxy,Method method,Object[] args){
if(args[0] instanceof ActionEvent){
elist.add((ActionEvent)args[0]);
String str=((ActionEvent)args[0]).getActionCommand();
System.out.print(str);
}
return null;
}
};
} public void addListener(Component c,EventSetDescriptor eventSet){
Object proxy=Proxy.newProxyInstance(null, new Class[]{eventSet.getListenerType()},handler);
Method addListenerMethod=eventSet.getAddListenerMethod();
try {
addListenerMethod.invoke(c, proxy);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
public void add(Component c){
try {
BeanInfo info=Introspector.getBeanInfo(c.getClass());
EventSetDescriptor[] eventSets=info.getEventSetDescriptors();
for(EventSetDescriptor eventSet:eventSets){
addListener(c,eventSet);
}
} catch (IntrospectionException e) {
}
if(c instanceof Container){
for(Component comp:((Container)c).getComponents()){
add(comp);
}
}
}
public void replay(CalculatorFrame f){
EventQueue eq = f.getToolkit().getSystemEventQueue();
for(ActionEvent aEvent:elist){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ActionEvent ae=new ActionEvent(aEvent.getSource(),aEvent.getID(),aEvent.getActionCommand(),System.currentTimeMillis(),aEvent.getModifiers());
System.out.println(ae);
eq.postEvent(ae);
}
}
public static void main(String[] args){
CalculatorFrame f=new CalculatorFrame();
EventTracer2 tracer=new EventTracer2();
tracer.add(f);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner input=new Scanner(System.in);
input.nextLine();
System.out.println("回放");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tracer.replay(f);
}
}