我有一个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)

07-24 18:26
查看更多