【问题标题】:JavaFX Controller not defined in root elementJavaFX 控制器未在根元素中定义
【发布时间】:2016-02-06 17:31:47
【问题描述】:

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

    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)

任何可能的解决方案?

【问题讨论】:

    标签: java javafx scenebuilder


    【解决方案1】:

    尝试改变:

    onAction="buttoncomOnAction"
    

    到

    onAction="#buttoncomOnAction"
    

    在 FXML 布局中的按钮元素中。

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

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

    【讨论】:

    • 现在它说:Controller not defined in root element
    • 谢谢!您介意解释这些更改或导致异常的原因吗?
    • 好吧,我不是一个 JavaFX 大师,但这似乎是 JavaFX 的基本语法和约定。如果您使用控制器将逻辑与视图分离,这就是您“附加”它们的方式,如果我可以这么说的话。
    • 这个答案中对我的问题有用的一点是“fx:controller”属性。这就是我在场景构建器中构建的场景中缺少的内容。
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多