java.lang.ClassNotFoundException无法到达控制器

时间:2019-07-02 18:33:19

标签: java javafx scenebuilder

我一直在使用Java,javafx和场景生成器来做自己的项目,很有趣。不幸的是,我遇到了无法解决的问题。我有一个名为“ Homepage”的fxml文件和一个名为HomepageController的控制器。 当我运行程序时,它给我一个错误'java.lang.ClassNotFoundException:HomepageController'。 我正在使用MVC架构,其中我的FXML文件位于名为“ views”的程序包中,而控制器位于另一个名为“ controllers”的程序包中 我一直在寻找类似的问题,但找不到解决方案。有人可以帮忙吗?

FXML文件

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>

<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="HomepageController">
   <children>
      <SplitPane dividerPositions="0.2505567928730512, 0.7505567928730512" focusTraversable="true" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
         <items>
            <AnchorPane>
               <children>
                  <Label alignment="CENTER" layoutX="14.0" layoutY="14.0" minWidth="60.0" prefWidth="-1.0" style="&#10;" text="Tournament" textAlignment="CENTER" wrapText="false">
                     <font>
                        <Font size="18.0" fx:id="x1" />
                     </font>
                     <textFill>
                        <Color blue="0.624" green="0.624" red="0.624" fx:id="x2" />
                     </textFill>
                  </Label>
                  <Button layoutX="58.0" layoutY="553.0" mnemonicParsing="false" onAction="#addTournament" text="Add Tournament" />
               </children>
            </AnchorPane>
            <ScrollPane prefHeight="-1.0" prefWidth="-1.0">
               <content>
                  <AnchorPane id="Content" minHeight="-1.0" minWidth="-1.0" prefHeight="592.0" prefWidth="430.0">
                     <children>
                        <Label alignment="CENTER" font="$x1" layoutX="14.0" layoutY="14.0" style="&#10;" text="Teams" textAlignment="CENTER" textFill="$x2" wrapText="false" />
                        <Button layoutX="180.0" layoutY="553.0" mnemonicParsing="false" onAction="#addTeam" text="Add Team" />
                     </children>
                  </AnchorPane>
               </content>
            </ScrollPane>
            <AnchorPane>
               <children>
                  <Label alignment="CENTER" font="$x1" layoutX="14.0" layoutY="14.0" style="&#10;" text="Players" textAlignment="CENTER" textFill="$x2" wrapText="false" />
                  <Button layoutX="74.0" layoutY="553.0" mnemonicParsing="false" onAction="#addPlayer" text="Add Player" />
               </children>
            </AnchorPane>
         </items>
      </SplitPane>
      <HBox id="HBox" alignment="CENTER_LEFT" spacing="5.0" VBox.vgrow="NEVER">
         <padding>
            <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
         </padding>
      </HBox>
   </children>
</VBox>

控制器

package controllers;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

public class HomepageController {

    @FXML
    private Font x1;

    @FXML
    private Color x2;

    @FXML
    void addPlayer(ActionEvent event) {
        System.out.println("Add Player Button");
    }

    @FXML
    void addTeam(ActionEvent event) {
        System.out.println("Add Team Button");
    }

    @FXML
    void addTournament(ActionEvent event) {
        System.out.println("Add Tournament Button");
    }

}

这就是控制台中显示的内容

javafx.fxml.LoadException: 
/C:/Users/joao/eclipse-workspace/Virtual%20Tournament/bin/views/Homepage.fxml:14

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    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 application.Main.start(Main.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: HomepageController
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 22 more

1 个答案:

答案 0 :(得分:0)

使用FXML文件时,FXML文件需要知道控制器文件和程序包名称。

<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.HomepageController">