另一个控制器类中的访问控制器

编程入门 行业动态 更新时间:2024-10-27 02:25:13
本文介绍了另一个控制器类中的访问控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的根布局包含2种布局:-OptionsPane -DrawArea

I have root layout containing 2 layouts: - OptionsPane - DrawArea

我正在尝试在OptionsPaneController中访问DrawAreaController进行调用它的绘制方法。下面是OptionsPaneController中的初始化方法:

What i am trying is to access DrawAreaController in OptionsPaneController to call its draw method. Below is initialize method from OptionsPaneController:

@Override public void initialize(URL url, ResourceBundle rb) { newDragonButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if(newDragonButton.getText().equals("Nowy")){ //do something } else{ newDragonButton.setText("Nowy"); FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DrawArea.fxml")); DrawAreaController dac = (DrawAreaController) loader.getController(); Dragon dragon = new Dragon(600, 300, 20, 2, 90, 270, colorChooser.getValue()); if(dac == null) System.out.println("controller = null"); dac.drawDragon(dragon); } } }); }

不幸的是dac始终为空。

Unfortunately dac is always null.

我的代码中是否存在任何错误,或者是否不可能在另一个控制器中加载控制器?

Is there any mistake in my code or is it impossible to load controller in another controller?

推荐答案

该控制器始终为null,因为您从不加载FXMLLoader。

The controller is always null, because you never load the FXMLLoader.

load() 加载fxml并为您实例化控制器实例。如果在 getController()之前不使用此方法,它将始终返回 null

The load() loads the fxml and instantiates the controller instance for you. If you do not use this method before getController(), it will always return null

FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DrawArea.fxml")); Parent root = loader.load(); DrawAreaController dac = (DrawAreaController) loader.getController();

您可能会或可能不想存储FXML的根。

更多推荐

另一个控制器类中的访问控制器

本文发布于:2023-11-17 04:24:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   类中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!