简单的 JavaFX HelloWorld 不起作用
Simple JavaFX HelloWorld Does Not Work
我仍然一遍又一遍地收到此错误: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) {
}
}
如有任何帮助,我们将不胜感激。
您缺少使标记可访问内容的@FXML 注释标记
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) {
}
}
没有注释就找不到
找到问题了。它是 ActionEvent class,在导入部分中声明的 class 不是 JavaFX class,因此使用正确的它可以正常工作。这是最终代码:
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!!");
}
}
不需要注释。
我仍然一遍又一遍地收到此错误: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) {
}
}
如有任何帮助,我们将不胜感激。
您缺少使标记可访问内容的@FXML 注释标记
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) {
}
}
没有注释就找不到
找到问题了。它是 ActionEvent class,在导入部分中声明的 class 不是 JavaFX class,因此使用正确的它可以正常工作。这是最终代码:
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!!");
}
}
不需要注释。