无法在JavaFX中加载Controller

时间:2018-01-13 08:58:27

标签: java javafx java-8

现在我正在通过JavaFX制作一个关于文件管理器软件的项目,但是会发生很多错误。

我有一些课程:

档案 Main.java

package QLF;

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("LogIn.fxml"));

        Scene scene = new Scene(root);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

档案 Controller.java

package QLF; 
import javafx.scene.control.*; 
import javafx.fxml.*; 
import javafx.event.*;
import java.net.URL; 
import java.util.ResourceBundle;

    public class Controller implements Initializable
    {
    @FXML
    private Button btnSignIn;

    @FXML
    private Button btnQuit;

    @FXML
    private TextField txtUserName;

    @FXML
    private PasswordField txtPassword;

    @FXML
    private Label lblUStatus;

    @FXML
    private Label lblPStatus;

    @FXML
    private void handleEvent(ActionEvent event)
    {
        if(event.getSource() == btnSignIn)
        {
            //check 'null'
            if(txtUserName.getText().equals(null))
            {
                lblUStatus.setVisible(true);
                lblUStatus.setText("You have to enter your Username or your Email");
            }

            else if(txtPassword.getText().equals(null))
            {
                lblPStatus.setVisible(true);
                lblPStatus.setText("You have to enter your Password");
            }
        }

        if(event.getSource() == btnQuit)
        {

        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

    }   
}

文件 LogIn.fxml

<?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 id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="376.167" prefWidth="614.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="QLF.Controller">
   <children>
      <Label id="title" layoutX="141.0" layoutY="55.0" prefHeight="73.0" prefWidth="315.0" text="Log-In to QLF">
         <font>
            <Font size="50.0" />
         </font>
      </Label>
      <TextField id="txtUserName" fx:id="txtUserName" layoutX="50.0" layoutY="150.0" prefHeight="25.0" prefWidth="521.0" promptText="Username*" />
      <Button id="btnSignIn" fx:id="btnSignIn" layoutX="50.0" layoutY="302.0" mnemonicParsing="false" onMouseClicked="#handleEvent" prefHeight="25.0" prefWidth="109.0" text="Sign-In" />
      <Button id="btnQuit" fx:id="btnQuit" layoutX="462.0" layoutY="302.0" mnemonicParsing="false" onMouseClicked="#handleEvent" prefHeight="25.0" prefWidth="109.0" text="Quit" />
      <PasswordField id="txtPassword" fx:id="txtPassword" layoutX="50.0" layoutY="243.0" prefHeight="25.0" prefWidth="521.0" promptText="Password*" />
      <Label fx:id="lblUStatus" layoutX="50.0" layoutY="180.0" prefHeight="17.0" prefWidth="33.0" text="Status" textFill="RED" visible="false" />
      <Label fx:id="lblPStatus" layoutX="53.0" layoutY="274.0" text="Status" textFill="#f50000" visible="false" />
   </children>
</AnchorPane>

错误

 Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
    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.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:3)

MouseEvent个事件导致ActionEvent不是MouseEvent。由于ActionEvent无法转换为handleEvent,因此MouseEvent无法处理onAction

改为使用... <Button ... onAction="#handleEvent" ... text="Sign-In" /> <Button ... onAction="#handleEvent" ... text="Quit" /> ... 事件处理程序:

LinearLayout