JavaFX:can控制器可以是一个抽象类? [英] JavaFX: can controller be an abstract class?
问题描述
我一直在这部分代码工作一天,只是不知道为什么它总是生成错误。
I have been working on this part of code for a day and just can't figure out why it always generates error.
我有一个控制器和FXML。他们工作完美。然后,我想到我想重用这个特定的控制器与抽象updateSelect()函数。因此,我将控制器更改为抽象。
I have a controller and FXML. They worked perfectly. Then it came to my mind that I want to reuse this particular controller with a abstract updateSelect() function. Therefore, I change the controller to abstract.
代码编译得很好。直到我尝试运行这部分代码。
The code compiled just fine. Until I try to run this part of code.
@FXML
private void mnuProjMember_onClick(ActionEvent event) {
mainContent.getChildren().clear();
FXMLLoader loader = new FXMLLoader(getClass().getResource("PaneProjectSearch.fxml"));
PaneProjectSearchController controller = new PaneProjectSearchController(){
@Override
void updateSelect(){
System.out.println("update: !!");
}
};
loader.setController(controller);
controller.setParent(mainContent);
fitToParent(loader);
}
它给了我以下错误信息。嗯...这是没有意义的,因为代码将再次正常工作后,我删除了抽象部分,甚至没有接触FXML或其他函数。
It gives me following error message. Well...it's non-sense because the code will work fine again after I remove the abstract parts without even touching the FXML or other functions.
错误解决onAction = #btnAdd_onClick',则事件处理程序不在命名空间中,或脚本中有错误。
file:/ D:/NetBeansWork/ProjCostTracking/dist/run1210215635/ProjCostTracking.jar!/ProjCostTracking/PaneProjectSearch.fxml:20
Error resolving onAction='#btnAdd_onClick', either the event handler is not in the Namespace or there is an error in the script. file:/D:/NetBeansWork/ProjCostTracking/dist/run1210215635/ProjCostTracking.jar!/ProjCostTracking/PaneProjectSearch.fxml:20
任何指导和建议欢迎,谢谢:)
Any guidance and advise is welcomed, thanks :)
推荐答案
我猜你在抽象控制器类中有一个私有处理程序方法。为了使这个工作,我认为处理程序方法,以及任何@ FXML注释字段,需要直接访问子类(即public或protected,或默认可见性,如果子类是在同一个包中的抽象控制器)。
I'm guessing that you have a private handler method in the abstract controller class. To make this work, I think the handler methods, as well as any @FXML-annotated fields, need to be directly accessible by the subclass (i.e. public or protected, or default visibility if the subclass is in the same package as the abstract controller).
这篇关于JavaFX:can控制器可以是一个抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!