我刚开始学习JavaFX。我想创建一个Login Scene,我的目标是编写一个库存应用程序。当我使用java 8在eclipse中运行以下代码时,我收到错误。请帮我。 fxml是使用场景构建器生成的。
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("LoginStage.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("HelloSwingNode Sample");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LoginStage.fxml:
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.util.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="467.0" prefWidth="716.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.Controller">
<children>
<Pane layoutX="31.0" layoutY="22.0" prefHeight="413.0" prefWidth="644.0" style="-fx-background-color: #ffffff;">
<children>
<JFXTextField fx:id="userName" cacheShape="false" focusColor="#8fc9f5" layoutX="123.0" layoutY="147.0" maxWidth="386.0" minWidth="210.0" prefHeight="41.0" prefWidth="386.0" promptText="Username" unFocusColor="BLACK">
<font>
<Font size="14.0" />
</font>
</JFXTextField>
<JFXPasswordField fx:id="password" focusColor="#8fc9f5" layoutX="123.0" layoutY="221.0" maxWidth="386.0" minWidth="205.0" prefHeight="41.0" prefWidth="386.0" promptText="Password">
<font>
<Font size="14.0" />
</font>
</JFXPasswordField>
<JFXButton fx:id="login" alignment="CENTER" buttonType="RAISED" layoutX="123.0" layoutY="313.0" onAction="#makeLogin" prefHeight="41.0" prefWidth="386.0" ripplerFill="WHITE" style="-fx-background-color: #9999ff;" text="Login" textAlignment="CENTER" textFill="#e5e5ff">
<font>
<Font name="System Bold" size="20.0" />
</font>
</JFXButton>a
<Label layoutX="123.0" layoutY="21.0" prefHeight="74.0" prefWidth="272.0" style="-fx-background-color: #ffffff;" text="Welcome" textFill="#8cc9ff">
<font>
<Font size="50.0" />
</font>
</Label>
<Label layoutX="123.0" layoutY="95.0" prefHeight="31.0" prefWidth="194.0" style="-fx-background-color: #ffffff;" text="To our inventory system" textFill="#949292">
<font>
<Font size="17.0" />
</font>
</Label>
</children>
<effect>
<DropShadow />
</effect>
</Pane>
</children>
</AnchorPane>
我的错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/Kelvinyu/workspace/InvApp/bin/application/LoginStage.fxml:13
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:16)
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
Caused by: java.lang.ClassNotFoundException: Main.Controller
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
Exception running application application.Main
答案 0 :(得分:0)
这里的关键是:
FXML:fx:controller="Main.Controller"
Stacktrace:Caused by: java.lang.ClassNotFoundException: Main.Controller
FXMLLoader
正试图找到你通过反射在FXML中声明的Controller类。我怀疑你没有班级Main.Controller
。
目前,FXMLLoader假设您的项目如下所示:
src
|-> Main (package)
| |-> Controller.java (class)
|-> Main.java (class)