JavaFX 控制器未在根元素中定义
JavaFX Controller not defined in root element
我是 JavaFX 的新手,我正在使用 SceneBuilder,它应该显示一个文本区域、一个按钮和一个列表。我有一个控制器 class :
package gui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
public class CommentaireController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private ListView LVCom;
@FXML
private Text
Area txtACom;
@FXML
private Button buttoncom;
@FXML
private void buttoncomOnAction (ActionEvent event)
{
txtACom.setText("test");
}
}
这是 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style=" -fx-background-color:	#C0C0C0;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<TextArea fx:id="actiontarget" depthTest="INHERIT" layoutX="14.0" layoutY="319.0" prefHeight="60.0" prefWidth="403.0" wrapText="true" />
<ListView layoutX="10.0" layoutY="14.0" prefHeight="272.0" prefWidth="579.0000999999975" />
<ScrollBar layoutX="573.0" layoutY="14.0" orientation="VERTICAL" prefHeight="272.0" />
<Button fx:id="buttoncom" layoutX="428.0" layoutY="326.0" mnemonicParsing="false" onAction="buttoncomOnAction" prefHeight="46.0" prefWidth="161.0" text="Commenter" textFill="#152020">
<font>
<Font name="Aharoni Bold" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
当我 运行 这个项目时,我得到这个异常日志:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication6(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Error resolving onAction='buttoncomOnAction', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/hadhe/Documents/NetBeansProjects/CrowdRisee/build/classes/gui/Commentaire.fxml:19
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at gui.main.start(main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null9(WinApplication.java:191)
... 1 more
Exception running application gui.main
C:\Users\hadhe\Documents\NetBeansProjects\CrowdRisee\nbproject\build-impl.xml:1039: The following error occurred while executing this line:
C:\Users\hadhe\Documents\NetBeansProjects\CrowdRisee\nbproject\build-impl.xml:804: Java returned: 1
BUILD FAILED (total time: 3 seconds)
有什么可能的解决方案吗?
尝试更改:
onAction="buttoncomOnAction"
至
onAction="#buttoncomOnAction"
在 FXML 布局中的按钮元素中。
此外,您需要在根元素上指定处理程序控制器:
<AnchorPane fx:controller="gui.CommentaireController" ...
我是 JavaFX 的新手,我正在使用 SceneBuilder,它应该显示一个文本区域、一个按钮和一个列表。我有一个控制器 class :
package gui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
public class CommentaireController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private ListView LVCom;
@FXML
private Text
Area txtACom;
@FXML
private Button buttoncom;
@FXML
private void buttoncomOnAction (ActionEvent event)
{
txtACom.setText("test");
}
}
这是 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style=" -fx-background-color:	#C0C0C0;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<TextArea fx:id="actiontarget" depthTest="INHERIT" layoutX="14.0" layoutY="319.0" prefHeight="60.0" prefWidth="403.0" wrapText="true" />
<ListView layoutX="10.0" layoutY="14.0" prefHeight="272.0" prefWidth="579.0000999999975" />
<ScrollBar layoutX="573.0" layoutY="14.0" orientation="VERTICAL" prefHeight="272.0" />
<Button fx:id="buttoncom" layoutX="428.0" layoutY="326.0" mnemonicParsing="false" onAction="buttoncomOnAction" prefHeight="46.0" prefWidth="161.0" text="Commenter" textFill="#152020">
<font>
<Font name="Aharoni Bold" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
当我 运行 这个项目时,我得到这个异常日志:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication6(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Error resolving onAction='buttoncomOnAction', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/hadhe/Documents/NetBeansProjects/CrowdRisee/build/classes/gui/Commentaire.fxml:19
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at gui.main.start(main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null9(WinApplication.java:191)
... 1 more
Exception running application gui.main
C:\Users\hadhe\Documents\NetBeansProjects\CrowdRisee\nbproject\build-impl.xml:1039: The following error occurred while executing this line:
C:\Users\hadhe\Documents\NetBeansProjects\CrowdRisee\nbproject\build-impl.xml:804: Java returned: 1
BUILD FAILED (total time: 3 seconds)
有什么可能的解决方案吗?
尝试更改:
onAction="buttoncomOnAction"
至
onAction="#buttoncomOnAction"
在 FXML 布局中的按钮元素中。
此外,您需要在根元素上指定处理程序控制器:
<AnchorPane fx:controller="gui.CommentaireController" ...