css - 无法在 javafx 8 中加载 css 文件

标签 css javafx

Scene listenMenu = new Scene(root, 250, 272);
listenMenu.getStylesheets().add("styles.css");

这对我加载我的 css 文件总是有用的,但是在一个小的 IntelliJ 更新之后它给了我这个错误:

Juni 20, 2018 1:16:45 NACHM. com.sun.javafx.css.StyleManagerloadStylesheetUnPrivileged WARNING: Resource "styles.css" not found.

我试图在这里寻找解决方案,但没有任何效果。像这样获取 url 和 toExternalForm():

listenMenu.getStylesheets().add(getClass().getResource("src/main/resources/styles.css").toExternalForm());

抛出这个异常:

    Exception in Application start method
Exception in thread "main" 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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
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.NullPointerException
    at HDMStuttgart.GUI.ListGUI.start(ListGUI.java:37)
    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)
    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:175)
    ... 1 more

元素结构: - 主要的 -- Java --- HDM斯图加特 ---- 界面 ----- ListGui.java <-- java 文件

-- 资源 --- styles.css <-- CSS文件

如有任何建议,我们将不胜感激!

最佳答案

试试这个:创建一个 Java 文件

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloStyledWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello Styled World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello Styled World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

        primaryStage.setTitle("Hello Styled World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

style.class 放在与 .java 文件相同的 package 中(如果您使用一个,则放在相应的资源文件夹中.

验证 build 是否将正确的 css 文件包含到输出文件夹中。 你可以

System.out.println(getClass().getResource("style.css").toExternalForm());

控制台查看 JavaFX 在哪里搜索 css 并检查 css 是否可用。

关于css - 无法在 javafx 8 中加载 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50947109/

相关文章:

javascript - 是否可以使用 css 覆盖单选按钮样式?

javascript - 使悬停菜单停留足够长的时间来点击

java - 手动关闭应用程序并调用方法? [JavaFX]

java - 任务只运行一次

java - JavaFX 中的图像碰撞/交叉

JavaFX 中的 CSS 样式在评论旁边不起作用

javascript - 如何在不丢失比例的情况下缩放背景图像

CSS 继承和 em 单位无法正确呈现

java - 如何在TestFX中测试多个场景

java - JxBrowser 6 在 osx el capitan 中挂起