java.lang.ClassNotFoundException 无法连接到控制器
java.lang.ClassNotFoundException Can't reach controller
我一直在做一个自己的项目,为了好玩,使用 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=" " 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=" " 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=" " 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.access0(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$launchApplication16(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait9(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null7(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater8(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$null2(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
使用 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">
我一直在做一个自己的项目,为了好玩,使用 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=" " 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=" " 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=" " 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.access0(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$launchApplication16(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait9(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null7(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater8(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$null2(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
使用 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">