我仍然一遍又一遍地收到此错误: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:
<?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) {
}
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
发现问题。它是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!!");
}
}
不需要注释。
答案 1 :(得分:0)
您缺少@FXML anotation标记以使内容可以访问标记
public class SampleController
{
@FXML
public Label helloWorld;
@FXML
public void sayHelloWorld(ActionEvent actionEvent)
{
//....
}
}
答案 2 :(得分:0)
像这样添加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) {
}
}
没有注释,就可以找到