[英]FXML and Controller in different packages - Button onAction issue
I'm new to JavaFX and currently having some trouble working with onAction events with classes within different packages. 我是JavaFX的新手,目前在使用不同包中的类处理onAction事件时遇到一些麻烦。
Here is the package tree : 这是包树:
Here is the sample of code that is not working : 以下是无效的代码示例:
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="GUIController.AccueilController"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
...
<HBox spacing="10" alignment="bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="Se connecter" onAction="#handleSubmitButtonAction"/>
</HBox>
...
</GridPane>
The error is sent by : 错误发送者:
onAction="#handleSubmitButtonAction"
Saying : "Handler method is not accessible. Make public, or annotate with @FXML" 说:“无法访问Handler方法。使用@FXML公开或注释”
Here is the AccueilController.java file : 这是AccueilController.java文件:
package GUIController;
import java.awt.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
public class AccueilController {
@FXML private Text actiontarget;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
actiontarget.setText("Sign in button pressed");
}
}
As you can see, the @FXML tag has been added, so I don't know where the problem is. 如您所见,已添加@FXML标记,因此我不知道问题出在哪里。 It may be a bit dumb, but I really can't figure it out. 这可能有点愚蠢,但我真的无法弄明白。
By the way, without the onAction line, the code is working perfectly. 顺便说一句,如果没有onAction行,代码就能完美运行。
Thank you guys ! 感谢你们 !
Try replacing import java.awt.event.ActionEvent
with import javafx.event.ActionEvent
. 尝试使用import javafx.event.ActionEvent
替换import java.awt.event.ActionEvent
。 JavaFX probably tries to call your method with another type of argument and can't find a suitable overloaded method. JavaFX可能尝试使用其他类型的参数调用您的方法,但无法找到合适的重载方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.