加载FXML时解决“ onAction”时出错

荒凉

我在运行一个简单的hello world应用程序时遇到一些问题。它抛出以下错误:

Exception in Application start method
Exception in thread "main" 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$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$2/1329552164.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#printHello', either the event handler is not in the Namespace or there is an error in the script.
/home/willi/java/IdeaProjects/JavaFXApp/out/production/JavaFXApp/sample/sample.fxml:23

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:606)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:766)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$57/2085742749.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/738441956.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$55/689939224.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$53/1382865734.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/704249306.run(Unknown Source)
    ... 1 more

我的Controller.java文件包含方法printHelloprintWorld在其中定义,我想在按相应按钮时调用它们。代码如下:包样本;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import java.awt.event.ActionEvent;

public class Controller {

    @FXML
    private Button helloButton;

    @FXML
    private Button worldButton;

    @FXML
    private void printHello(ActionEvent e){
        System.out.println("Hello");
    }

    @FXML
    private void printWorld(ActionEvent f){
        System.out.println("World");
    }
}

在我的fxml文件中,我定义了两个按钮。他们的代码如下:

<Button fx:id="helloButton" layoutX="46.0" layoutY="87.0" mnemonicParsing="false" onAction="#printHello" text="Hello" />

<Button fx:id="worldButton" layoutX="97.0" layoutY="87.0" mnemonicParsing="false" onAction="#printWorld" text="World" />

为什么我的代码抛出此错误?

抱歉,格式不好,这是我第一次发布。

乌鲁克·比(Uluk Biy)

ActionEvent在控制器类中导入了错误

import java.awt.event.ActionEvent;

更正为

import javafx.event.ActionEvent;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

GraalVM javafx.fxml.LoadException:解决onAction ='#loginAction'时出错,事件处理程序不在命名空间中

来自分类Dev

在JavaFX项目中加载fxml文件时出错

来自分类Dev

在JavaFX项目中加载fxml文件时出错

来自分类Dev

解决食谱时出错

来自分类Dev

加载 fxml 时出现“javafx.fxml.LoadException”

来自分类Dev

加载WebappClassLoader时出错

来自分类Dev

加载FacesContextFactory时出错

来自分类Dev

渴望加载时出错

来自分类Dev

加载图像时出错

来自分类Dev

加载SoX时出错

来自分类Dev

加载 webview 时出错

来自分类Dev

加载模块枕头时出错

来自分类Dev

加载YAML文件时出错

来自分类Dev

加载ipython qtconsole时出错

来自分类Dev

GeoTools,加载地图时出错

来自分类Dev

监听宝石加载时出错

来自分类Dev

加载共享库时出错

来自分类Dev

加载程序集时出错

来自分类Dev

加载Oracle库时出错

来自分类Dev

加载 .json/.csv 时出错

来自分类Dev

Javascript:加载页面时出错

来自分类Dev

从资产加载图像时出错

来自分类Dev

加载FXML文件时出现“需要位置”异常

来自分类Dev

在Eclipse外部运行jar时未加载FXML布局

来自分类Dev

加载FXML文件时出现“需要位置”异常

来自分类Dev

加载fxml时为什么会出现stackoverflow?

来自分类Dev

在Eclipse外部运行jar时未加载FXML布局

来自分类Dev

传递参数时加载css时出错

来自分类Dev

JSONSchema-解决架构参考时出错