我仍然一遍又一遍地收到此错误:Error resolving onAction='#sayHelloWorld', either the event handler is not in the Namespace or there is an error in the script.。我已经在Internet上搜索了一个解决方案,但没有任何效果,肯定是一个小细节,因为我不熟悉JAvaFX,这是我的第一个HelloWorld应用程序。无论如何,这是我正在使用的代码:
Error resolving onAction='#sayHelloWorld', either the event handler is not in the Namespace or there is an error in the script.
sample.fxml:
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.geometry.Insets?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController"> <columnConstraints> <ColumnConstraints /> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> <RowConstraints /> </rowConstraints> <children> <Button text="Load News" GridPane.columnIndex="1" GridPane.rowIndex="1" onAction="#sayHelloWorld"/> <Label GridPane.columnIndex="0" GridPane.rowIndex="1" fx:id="helloWorld"/> </children> </GridPane>
还有 SampleController.java
package sample; import javafx.scene.control.Label; import java.awt.event.ActionEvent; public class SampleController { public Label helloWorld; public void sayHelloWorld(ActionEvent actionEvent) { } }
任何帮助,将不胜感激。
找到了问题。它是ActionEvent类,在import部分中声明的类不是JavaFX类,因此使用正确的类可以使其正常工作。这是最终代码:
package sample; //import java.awt.event.ActionEvent; //This was the error! import javafx.event.ActionEvent; import javafx.scene.control.Label; public class SampleController { public Label helloWorld; public void sayHelloWorld(ActionEvent actionEvent) { helloWorld.setText("Hello World!!"); } }
不需要注释。