问题描述
我正在跟着学校的录像一起发誓,我发誓我添加了他们告诉我添加的所有内容,但我一直遇到问题。我唯一能想到的是,视频中的人正在运行Netbeans 11时正在运行Netbeans 8.2,并且我知道JavaFX在较新版本的Netbeans上并不那么容易...
我的项目一直运行,直到我向“主菜单”按钮添加操作,并且我不明白为什么它停止运行。这就是我的错误输出说的:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#onActionCreateAnimal',either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/ajw51/Documents/NetBeansprojects/example123/build/classes/view/MainMenu.fxml:16
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at example123.Example123.start(Example123.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.invokelaterdispatcher$Future.run(invokelaterdispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application example123.Example123
C:\Users\ajw51\Documents\NetBeansprojects\example123\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\ajw51\Documents\NetBeansprojects\example123\nbproject\build-impl.xml:936: Java returned: 1
BUILD Failed (total time: 2 seconds)
这是我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefheight="200.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainMenuController">
<children>
<VBox alignment="CENTER" prefheight="200.0" prefWidth="320.0" spacing="10.0">
<children>
<Button mnemonicParsing="false" onAction="#onActionCreateAnimal" prefheight="50.0" prefWidth="100.0" text="CREATE">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#onActiondisplayAnimals" prefheight="50.0" prefWidth="100.0" text="disPLAY">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#onActonExit" prefheight="50.0" prefWidth="100.0" text="EXIT">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Button>
</children>
</VBox>
</children>
</AnchorPane>
这是我的控制器:
/*
* To change this license header,choose License Headers in Project Properties.
* To change this template file,choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* @author ajw51
*/
public class MainMenuController implements Initializable {
@FXML
void onActionCreateAnimal(ActionEvent event) {
}
@FXML
void onActiondisplayAnimals(ActionEvent event) {
}
@FXML
void onActonExit(ActionEvent event) {
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url,ResourceBundle rb) {
// Todo
}
}
这是我的主要爱好:
/*
* To change this license header,choose Tools | Templates
* and open the template in the editor.
*/
package example123;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author ajw51
*/
public class Example123 extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Todo code application logic here
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/MainMenu.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Main Menu");
stage.show();
}
}
我先感谢您的帮助!
解决方法
我发现了我的问题所在!我输入了错误的内容。应该是:
import javafx.event.ActionEvent;
代替:
import java.awt.event.ActionEvent;