我有一个JComboBox,它具有通过classLoader动态加载的Java类。创建对象后,我想以arg形式传递给方法之一JTextField [] [[]。
final JTextField[][] gameFields = new JTextField[12][12];
Object runtimeStrategyObject = strategyClass.newInstance();
Method method = strategyClass.getDeclaredMethod("move",JTextField[][].class);
method.invoke(runtimeStrategyObject, gameFields);
我要呼叫的方法
public void move(JTextArea[][] gameFields) {
// method body
}
问题是我得到“ NoSuchMethodException”。任何想法如何解决?
最佳答案
您的move(JTextArea[][] gameFields)
方法将JTextArea[][].class
作为参数类型。因此,正确的方法应该是通过传递getDeclaredMethod
作为参数类型来尝试使用JTextArea[][].class
获取函数:
Method method = strategyClass.getDeclaredMethod("move", JTextArea[][].class);
或者,将
move
方法参数类型更改为JTextField[][]
:public void move(JTextField[][] gameFields) {
// method body
}
查看文档:
getDeclaredMethod(String name, Class<?>... parameterTypes
)