JavaFX Controller 未在根元素中定义

标签 java javafx scenebuilder

我是 JavaFX 新手,我正在使用 SceneBuilder,它应该显示一个文本区域、一个按钮和一个列表。我有一个 Controller 类:

    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="&#10;  -fx-background-color:&#9;#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$launchApplication$156(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.access$100(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$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$null$149(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 布局中的按钮元素中。

另外,您需要在根元素上指定处理程序 Controller :

<AnchorPane fx:controller="gui.CommentaireController" ...

关于JavaFX Controller 未在根元素中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244121/

相关文章:

java - 如何从Spring查询dsl谓词中提取表达式路径和值?

java - 如何在 Google App Engine 上读取 Excel 文件

java - GWT 设计器 Eclipse

javafx - 从 TextField JavaFX 中删除默认焦点

javafx - 矩形碰撞时的圆运动

java - com.gluonhq.charm.glisten.control.TextField 不存在

javafx - 如何在 Gridpane 中获得相同垂直尺寸的 TitledPanes? (JavaFX、场景构建器)

java - 为什么 JAXB 生成的类具有 protected 成员,我该如何更改它?

JavaFX 不通过 stop() 方法停止应用程序

JavaFX 默认元素