gpt4 book ai didi

java - 如何将 JTextField[][] 作为参数传递给动态加载的 java 类方法?

转载 作者:行者123 更新时间:2023-11-30 04:09:23 26 4
gpt4 key购买 nike

我有一个 JComboBox,其中通过 classLoader 动态加载 java 类。创建对象后,我想将其方法之一 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 )

关于java - 如何将 JTextField[][] 作为参数传递给动态加载的 java 类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018693/

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