gpt4 book ai didi

java - 如何创建一个只有文本名称的类的数组?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:52:17 25 4
gpt4 key购买 nike

我目前正在创建一个库,旨在查看一堆字符串并使用它们创建窗口。目前,只有几行代码和一个巨大的配置文件会引发大量的窗口和东西。我将其设置为读取一些字符串并从中加载类,以获取方法的参数。这是执行此操作的方法:

    private Class<?> getClass(String classname){
switch(classname){//Check for some primitive type references, since they don't work in the below area
case "int":
return int.class;
case "double":
return double.class;
case "boolean":
return boolean.class;
case "long":
return long.class;
case "short":
return short.class;
}
Class<?> value = null;
try{
value = Config.class.getClassLoader().loadClass(classname);
}catch(ClassNotFoundException ex){
Error.error(ErrorLevel.severe, "Could not find class- "+classname, ex, ErrorCatagory.classFinding);//A custom error handling system
}
return value;//Return the class found from the name
}

它在普通类(class)上工作得很好。例如,如果我将 java.lang.String 放入其中,它会将 String 类吐回给我。如果我输入 javax.swing.JTextField,它会向我返回正确的类。但是,如果我将 java.lang.String[] 放入其中以请求一个字符串数组,它就会崩溃,如下所示:

    Warning in catagory code- message = Could not find suitable instance for method- Expected JTextComponent but found JTextField!
Severe error in catagory classFinding
May 16, 2012 11:02:35 AM ErrorAdapter severeError
SEVERE: Could not find class- java.lang.String[]
java.lang.ClassNotFoundException: java.lang.String[]
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Config.getClass(Config.java:389)
at Config.getClasses(Config.java:398)
at Config.methodCall(Config.java:492)
at Config.call(Config.java:415)
at Config.chainCall(Config.java:439)
at Config.call(Config.java:409)
at main.action(main.java:122)
at main$1$5.actionPerformed(main.java:63)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

第一行 - 以“Warning”开头的行 - 是因为如果我尝试在 JTextField.class 上使用 Class.getDeclaredMethod() 来获取那里的 getText() 方法,它会崩溃。剩下的就是崩溃报告。

有没有一种方法可以解决这个问题并使其适用于任何类/数组深度(因此它适用于 java.lang.String[]、javax.swing.JTextField[][][]、和 int[][] 没有对它们中的任何一个进行硬编码)?

另外,附带说明一下,是否有更好的方法来获取基于字符串的基本类型的类?

最佳答案

关于java - 如何创建一个只有文本名称的类的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10621563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com