JavaFX 运行时异常

JavaFX RunTime Exception

我刚刚使用 SceneBuilderfxml 设计了一个界面。当我 运行 程序时,它给了我错误。我可以知道这个程序有什么问题吗?

   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:464)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        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:1051)
    Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:832)
    Caused by: javafx.fxml.LoadException: 
    /C:/Users/tariq/IdeaProjects/PCIapp/target/classes/com/PCIapp/primary.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2949)
    at javafx.fxml/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2793)
    at javafx.fxml/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2758)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
    at com.PCIapp/com.PCIapp.App.loadFXML(App.java:32)
    at com.PCIapp/com.PCIapp.App.start(App.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    ... 1 more
    Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXPasswordField
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3017)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3006)
        at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2947)
        ... 16 more
    Exception running application com.PCIapp.App

这里是 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>
    
    <?import com.jfoenix.controls.JFXPasswordField?>
    <?import com.jfoenix.controls.JFXTextField?>
    <?import javafx.scene.image.Image?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.Pane?>
    <?import javafx.scene.text.Font?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" opacity="0.6" prefHeight="800.0" prefWidth="1280.0" style="-fx-background-color: #F5F6FA;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <ImageView fitHeight="634.0" fitWidth="1204.0" layoutX="38.0" layoutY="83.0" pickOnBounds="true" preserveRatio="true" rotate="-8.0">
             <image>
                <Image url="@PCI%20logoRYB.png" />
             </image></ImageView>
          <Pane layoutX="380.0" layoutY="140.0" prefHeight="521.0" prefWidth="520.0" style="-fx-background-color: #DFE4EA; -fx-border-radius: 20; -fx-background-radius: 20;">
             <children>
                <JFXTextField fx:id="Username" focusColor="#292f5f" labelFloat="true" layoutX="44.0" layoutY="96.0" prefHeight="69.0" prefWidth="432.0" promptText="Username" unFocusColor="#292f5f">
                   <font>
                      <Font name="Segoe UI Bold" size="20.0" />
                   </font>
                </JFXTextField>
                <JFXPasswordField fx:id="Password" focusColor="#292f5f" labelFloat="true" layoutX="44.0" layoutY="294.0" prefHeight="69.0" prefWidth="432.0" promptText="Password" unFocusColor="#292f5f">
                   <font>
                      <Font name="Segoe UI Bold" size="20.0" />
                   </font>
                </JFXPasswordField>
             </children>
          </Pane>
       </children>
    </AnchorPane>

这是控制器“它是我在创建项目时预先编写的,直到现在才对其进行编辑”

    package com.PCIapp;

import java.io.IOException;
import javafx.fxml.FXML;

public class PrimaryController {

    @FXML
    private void switchToSecondary() throws IOException {
        App.setRoot("secondary");
    }
}

这里是主文件

   package com.PCIapp;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 1280, 800);
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

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

}

终于找到答案了

它在模块中-info.java 文件

我只需要将这行代码放在那里

requires com.jfoenix;

但要小心一点

当我 运行 使用不同的 Jfoenix 组件的项目时,假设我遇到了“java.lang.reflect.InaccessibleObjectException”

具有大量堆栈跟踪

有人在这里遇到了同样的事情 https://github.com/jfoenixadmin/JFoenix/issues/955

对我来说,因为我在 IntelliJ 上使用 maven,所以我在 VM 选项中添加了这两行

    --add-opens
java.base/java.lang.reflect=com.jfoenix
--illegal-access=warn

一切正常