简单的JavaFX HelloWorld不起作用
我仍然一遍又一遍地得到这个错误: Error resolving onAction='#sayHelloWorld', either the event handler is not in the Namespace or there is an error in the script.
。 我已经在互联网上搜索了一个解决方案,但没有任何作用,当然我是一个小细节,因为我是JAvaFX的新手,这是我的第一个HelloWorld应用程序。 无论如何,这是我正在使用的代码:
sample.fxml:
和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!!"); } }
不需要注释。
您缺少@FXML anotation标记以使内容可以访问标记
public class SampleController { @FXML public Label helloWorld; @FXML public void sayHelloWorld(ActionEvent actionEvent) { //.... } }
像这样添加fxml注释
package sample; import javafx.scene.control.Label; import java.awt.event.ActionEvent; public class SampleController { public Label helloWorld; @FXML public void sayHelloWorld(ActionEvent actionEvent) { } }
没有注释,它可以找到