无法在JavaFX 8中加载CSS文件 [英] Can't load css file in javafx 8

查看:308
本文介绍了无法在JavaFX 8中加载CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这始终对我加载CSS文件有用,但是在IntelliJ小更新后,它给了我这个错误:

This always worked for me to load my css file, but after a small IntelliJ update it gives me this error:

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

我试图在此处寻找解决方案,但没有任何效果.像这样使用url和toEx​​ternalForm():

I tried to look for a solution on here but but nothing works. Taking the url and the toExternalForm() like this:

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 --- HDMStuttgart ---- GUI ----- ListGui.java<-Java文件

Project Stucture: - main -- java --- HDMStuttgart ---- GUI ----- ListGui.java <-- The java file

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

-- resources --- styles.css <-- the css file

任何建议都会得到高度赞赏!

Any suggestions would be highly appretiated!

推荐答案

尝试一下:创建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中(如果使用的话,也可以放入相应的资源文件夹中.

put the style.class in the same package as the .java file (or in the corresponding resource folder if you are using one.

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

verify that the build includes the correct css file into output folder. you can

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

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

to console to see where JavaFX is searching for the css and check if the css is available there.

这篇关于无法在JavaFX 8中加载CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆