我试图让Java Applet中的模态框架如下所示:http://www.java2s.com/Tutorial/Java/0240__Swing/Showthegivenframeasmodaltothespecifiedowner.htm。这段代码有start()函数,看起来像java.lang.illegalArgumentException当使用反射调用pumpEvents
public void start() throws Exception {
Class<?> clazz = Class.forName("java.awt.Conditional");
Object conditional = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
this);
Method pumpMethod = Class.forName("java.awt.EventDispatchThread").getDeclaredMethod(
"pumpEvents", new Class[] { clazz });
pumpMethod.setAccessible(true);
pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
}.
当我打电话
pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
我有例外如下:
java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class
at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:136)
at wizard.WizardCore.showWizardFrame(WizardCore.java:206)
at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:55)
at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:35)
at java.security.AccessController.doPrivileged(Native Method)
at SelfRegistrationApplet.RunSelfRegistrationApplet(SelfRegistrationApplet.java:32)
at SelfRegistrationApplet.init(SelfRegistrationApplet.java:26)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at wizard.ModalFrameUtil$EventPump.start(ModalFrameUtil.java:80)
at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:133)
... 8 more
你能告诉什么是错在这个调用以及如何避免此异常?
__invoke__方法的第一个参数应该是要调用它的对象。在这里你传递__Thread.currentThread()__。那是对的吗?我宁愿使用__java.awt.EventDispatchThread__的实例来代替... – reef
** @ reef **,谢谢你的回答。但我有另一个 - 我怎么能得到一个'java.awt.EventDispatchThread'的实例?再次使用反射? – GPAshka
我解决了我的调用问题。原因在于线程类。我添加了获取java.awt.EventDispatchThread类的对象的代码,现在使用此对象调用invoke()metod:构造函数constructor = Class.forName(“java.awt.EventDispatchThread”)。 getDeclaredConstructor(ThreadGroup.class,name.getClass(),eventQueue.getClass()); 构造函数。setAccessible(真); 对象eventDispatchThread = constructor.newInstance(threadGroup,name,eventQueue);' – GPAshka