JavaFX .getResource方法

时间:2019-06-07 02:39:09

标签: java javafx intellij-14

我是Java的新手,但我在Intellij中不断收到错误消息,说我的启动方法有问题。

我知道问题是因为它不喜欢该路径,但是我将完整路径放入了login.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/view/login.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 700, 400));
        primaryStage.show();
    }


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

这也是我的login.xml文件代码:


<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="700.0" style="-fx-background-color: d32f2f;" xmlns="http://javafx.com/javafx/8.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.LoginController">
    <children>
        <AnchorPane layoutY="72.0" prefHeight="328.0" prefWidth="700.0" style="-fx-background-color: #ffffff;">
            <children>
                <JFXTextField fx:id="loginUserName" focusColor="#424242" layoutX="214.0" layoutY="67.0" prefHeight="27.0" prefWidth="272.0" promptText="Username" unFocusColor="#ff6659" />
                <JFXPasswordField fx:id="loginPassword" focusColor="#424242" layoutX="214.0" layoutY="137.0" prefHeight="27.0" prefWidth="272.0" promptText="Password" unFocusColor="#ff6659" />
                <Label alignment="CENTER" layoutX="285.0" layoutY="14.0" prefHeight="17.0" prefWidth="130.0" text="Login" textFill="#6f6767">
                    <font>
                        <Font size="18.0" />
                    </font>
                </Label>
                <JFXButton fx:id="loginButton" layoutX="269.0" layoutY="193.0" prefHeight="27.0" prefWidth="163.0" style="-fx-background-color: #424242;" text="Login" textFill="#fffefe" />
            </children>
        </AnchorPane>
        <Label alignment="CENTER" layoutX="285.0" layoutY="14.0" prefHeight="17.0" prefWidth="130.0" text="TO DO" textFill="#fcfafa">
            <font>
                <Font size="36.0" />
            </font>
        </Label>
        <JFXButton fx:id="loginSignUpButton" layoutX="598.0" layoutY="14.0" prefHeight="27.0" prefWidth="88.0" style="-fx-background-color: #9a0007;" text="Sign Up" textFill="WHITE" />
    </children>
</AnchorPane>

这是我得到的错误:

"Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/converters/PaintConverter
    at com.jfoenix.controls.JFXTextField$StyleableProperties.<clinit>(JFXTextField.java:266)
    at com.jfoenix.controls.JFXTextField.<init>(JFXTextField.java:171)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/java.lang.Class.newInstance(Class.java:558)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.converters.PaintConverter
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 25 more
Exception running application sample.Main
"

关于如何解决的任何想法?

1 个答案:

答案 0 :(得分:0)

您发布的堆栈跟踪的这一部分指示问题所在。

  

由于:java.lang.ClassNotFoundException:   sample.controller.LoginController

javadoc 中获取ClassNotFoundException

  

当应用程序尝试通过其字符串加载类时抛出   名称使用:

     
      
  • 类Class中的forName方法。
  •   
  • ClassLoader类中的findSystemClass方法。
  •   
  • ClassLoader类中的loadClass方法。
  •   
     

,但不能为具有指定名称的类定义   找到。

换句话说,找不到文件sample\controller\LoginController.class
请注意,上述路径是相对路径,并且是相对于应用程序的工作目录的。
工作目录是System属性“ user.dir”的值,如

String workingDirectory = System.getProperty("user.dir");

因此您可以检查为什么会收到异常。
没有这样的文件LoginController.class或它在错误的目录中。