我制作了一个小型应用程序,它从文件夹加载类,将它们显示在列表中,当您单击一个时,它会在其中显示JPanel
。
所以我做了一个 API 类 ( BaseApp
) 扩展JPanel
。我还制作了一个测试类extends BaseApp
并使用其中的 API。
现在,我的问题是每次我点击时TestApp
,它都会给我这个Exception
......
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Class cannot be cast to me.Delocaz.SuperApp.BaseApp
at me.Delocaz.SuperApp.AppSelect$2.mouseClicked(AppSelect.java:76)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我试图将Class
我从类加载器中获得的内容转换为BaseApp
. 我该怎么做才不会崩溃?
如果你需要它,这是我的FileClassLoader
:
package me.Delocaz.SuperApp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileClassLoader extends ClassLoader {
public Class<?> findClass(File f) {
byte[] b = loadClassData(f);
try {
return defineClass(null, b, 0, b.length);
} catch (ClassFormatError ex) {
return null;
}
}
private byte[] loadClassData(File f) {
FileInputStream fin;
byte fileContent[] = new byte[(int)f.length()];
try {
fin = new FileInputStream(f);
fin.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}
}
此外,这也是强制转换发生的地方:
Object c = cl.findClass(new File(***CLASS FILE LOCATION***));
System.out.println(c);
switchApp((BaseApp) c);