【JAVAFX】controller中反射调用@FXML的点击事件失败
场景
当前有一个controller中定义的事件如
@FXMLvoid openZhengjieWindow(ActionEvent event) {System.out.println("zhengjie");}
通过反射去调用
public void callMethodByString(String methodSuffix) {try {Method method = this.getClass().getMethod("open" + methodSuffix+"Window");method.invoke(this);} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {System.out.println("调用方法失败: " + e.getMessage());}}
报错失败调用方法失败: sample.ControllerAutoLogin.openWodeWindow()
解决方法
把controller定义事件的参数修改为无,同时增加public声明
@FXMLpublic void openZhengjieWindow() {System.out.println("zhengjie");}