带有scenebuilder的Java FX:运行应用程序Start.Main的异常

时间:2017-12-24 20:35:56

标签: java javafx scenebuilder

因为我使用scenebuilder将方法从控制器类连接到Login.fxml文件,所以我遇到了这个错误。 login.fxml文件中的register按钮应该重定向到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 个答案:

答案 0 :(得分:0)

login.fxml的控制器应该有一个名为regBtnClicked的方法。

@FXML private void regBtnClicked(ActionEvent event)
{
   ....
}