当前位置: 首页 > 知识库问答 >
问题:

Java FX with scenebuilder:运行应用程序启动时出现异常。主[副本]

郗缪文
2023-03-14

自从我将方法从控制器类连接到登录名后,我就出现了这个错误。使用scenebuilder的fxml文件。登录中的注册按钮。fxml文件应该重定向到regForm。fxml接口,但出现错误。

“主”类代码

package sample;

        import javafx.application.Application;
        import javafx.fxml.FXMLLoader;
        import javafx.scene.Parent;
        import javafx.scene.Scene;
        import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("UGNiP Education portal");
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

“控制器”类代码

package sample;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.awt.event.ActionEvent;
import java.io.IOException;

public class Controller {

    public void regBtnClicked(ActionEvent event) throws IOException {

        Parent regForm = FXMLLoader.load(getClass().getResource("regForm.fxml"));
        Scene regScene = new Scene(regForm);

        // To get the stage info
        Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();


        window.setScene(regScene);
        window.show();
    }
}

登录页面

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="294.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Label alignment="TOP_CENTER" layoutX="126.0" text="UGNiP Login Page">
         <font>
            <Font size="43.0" />
         </font>
      </Label>
      <Label layoutX="66.0" layoutY="78.0" prefHeight="17.0" prefWidth="44.0" text="I.D: ">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <TextField layoutX="126.0" layoutY="74.0" promptText="123456789" />
      <Label layoutX="309.0" layoutY="78.0" prefHeight="17.0" prefWidth="65.0" text="Password:">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <PasswordField layoutX="401.0" layoutY="74.0" promptText="******" />
      <Button layoutX="253.0" layoutY="188.0" mnemonicParsing="false" text="Login" />
      <Button layoutX="344.0" layoutY="188.0" mnemonicParsing="false" onAction="#regBtnClicked" text="Register" />
   </children>
</AnchorPane>

注册表XML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <TextField layoutX="130.0" layoutY="119.0" promptText="John" />
        <TextField layoutX="408.0" layoutY="119.0" promptText="Doe" />
        <TextField layoutX="130.0" layoutY="175.0" promptText="123456789" />
        <PasswordField layoutX="408.0" layoutY="175.0" promptText="********" />
        <TextField layoutX="130.0" layoutY="224.0" promptText="example@example.com" />
        <TextField layoutX="408.0" layoutY="224.0" promptText="0771234567" />
        <Label layoutX="36.0" layoutY="123.0" text="First Name: " />
        <Label layoutX="15.0" layoutY="179.0" text="Identification code: " />
        <Label layoutX="52.0" layoutY="228.0" text="Email:" />
        <Label layoutX="322.0" layoutY="123.0" text="Last Name: " />
        <Label layoutX="325.0" layoutY="179.0" text="Password: " />
        <Label layoutX="299.0" layoutY="228.0" text="Telephone Number: " />
        <Button layoutX="244.0" layoutY="298.0" mnemonicParsing="false" text="Confirm Registration " />
        <Button layoutX="32.0" layoutY="354.0" mnemonicParsing="false" text="Back" />
        <Label layoutX="225.0" layoutY="42.0" text="Registration ">
            <font>
                <Font size="30.0" />
            </font>
        </Label>
        <Button layoutX="248.0" layoutY="354.0" mnemonicParsing="false" text="Cancel Registration " />
    </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:498)
    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:498)
    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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#regBtnClicked', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/muizu/Documents/codes/cwk3/out/production/cwk3/sample/Login.fxml:31

    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 sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Exception running application sample.Main

共有1个答案

柳墨一
2023-03-14

您的login.fxml控制器中应该有一个名为regBtn点击方法

@FXML private void regBtnClicked(ActionEvent event)
{
   ....
}
 类似资料:
  • 当运行我的Spring启动应用程序时,启动时低于异常。不确定是什么依赖导致了这一点。 Spring启动版本-2.3.1 IDE-Intellij 这是一个多模块maven项目。如果需要其他信息,请告诉我。 下面是我的父母pom.xml 4.0.0

  • 我的主 LoginController.java 请注意,我还没有在userController.java中编写任何代码,我只是为user.fxml编写了ui 在javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2597)在javafx.fxml.fxmlloader.access$100(fxmlloader.java:1

  • 乍一看,这个问题似乎是重复的。我已经在谷歌搜索了一些,但不幸的是,没有一个结果不符合我。我给出了下面的问题链接。 应用程序启动方法java.lang.Reflect.InvocationTargetException JavaFX图像转换中出现异常 JavaFX-应用程序启动方法中的异常? 应用程序启动方法中出现异常 堆栈跟踪: 无法从此StackTrace跟踪错误。然后我在start方法中使用了

  • 我在启动Spring Boot应用程序时遇到以下异常。Spring Boot版本:2.1.13 Spring Framework版本:5.1.14 原因:原因:com.datastax.driver.core.exceptions.driverinternalerror:在类路径中检测到不兼容的Guava版本。你需要16.0.1或更高。在com.datastax.driver.core.guava

  • login.fxml: 它是在Eclipse中使用JRE 1.8.0而不是IntelliJ思想 这是一个错误:

  • 得到 错误[SpringApplication]应用程序启动失败org.springframework.beans.factory.BeanCreationException:创建名称为myEntityManagerFactory的bean错误定义在类路径资源[com/员工/服务/配置/DBConfiguration.class]:调用init方法失败;嵌套异常是java.lang.NullPoi