向intellijjavafx项目添加css表

u4dcyp6a  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(404)

这个问题在这里已经有了答案

如何确定我的javafx应用程序所需的fxml文件、css文件、图像和其他资源的正确路径(1个答案)
上个月关门了。
我正在尝试将css添加到我正在intellij中开发的javafx项目中。我有一个名为“list view.css”的css样式表,并希望将其附加到我的项目中。以下是我的项目目录截图:
项目目录
该文件与我附加代码的主文件位于同一目录中。当我打印出变量url的值时,我不断得到“null”并不断得到以下错误:

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:566)
    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:566)
    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$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
    at com.kernaldrive.startupscreen.Main.start(Main.java:72)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$3(WinApplication.java:174)
    ... 1 more
Exception running application com.kernaldrive.startupscreen.Main

我检查了这个答案:如何确定我的javafx应用程序所需的fxml文件、css文件、图像和其他资源的正确路径?
但不幸的是,这并不能解决问题。这是我的代码,任何帮助都将不胜感激:

public void start(Stage primaryStage) throws Exception {

    primaryStage.initStyle(StageStyle.UTILITY);     //removes minimize and fullscreen button

    Rectangle2D screenBounds = Screen.getPrimary().getBounds();
    double screenWidth = screenBounds.getWidth();
    double screenHeight = screenBounds.getHeight();
    primaryStage.setTitle("Homepage - KernalDrive");

    Pane layout = new Pane();

    Scene scene = new Scene(layout, 1920, 1080);
    URL url = getClass().getResource("listView.css");
    String css = url.toExternalForm();
    scene.getStylesheets().add(css);

    primaryStage.setScene(scene);
    primaryStage.setMaximized(true);
    primaryStage.show();
}

编辑:我已经修复了url=getclass().getresource(“listview.css”);但我还是得到同样的问题!

0s0u357o

0s0u357o1#

必须键入与文件相同的名称:
您键入了listview.css,但您的文件名为list-view.css
现在您的代码如下所示:

URL url = getClass().getResource("list-view.css");

相关问题