CSS 将css文件添加到javafx中的样式表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13946372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:05:22  来源:igfitidea点击:

adding css file to stylesheets in javafx

cssstylesheetjavafx

提问by Danielle

Language: JavaFX

语言:JavaFX

IDE: Netbeans

IDE:Netbeans

Problem: I'm trying to add a css file to the stylesheet, but the first line of the following code always generates a NullPointerException:

问题:我正在尝试向样式表添加一个 css 文件,但以下代码的第一行总是生成一个NullPointerException

String css = this.getClass().getResource("double_slider.css").toExternalForm(); 
scene.getStylesheets().add(css);

I've tried replacing "double_slider.css" with the full path. double_slider.css is currently located in the same package as the class that makes this call. I've also tried all of the variations found at http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/, with no success. Clean and build doesn't help either.

我试过用完整路径替换“double_slider.css”。double_slider.css 当前与进行此调用的类位于同一个包中。我还尝试了在http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/ 上找到的所有变体,但没有成功。清洁和构建也无济于事。

If I place the css file in the build folder where the .class files are dumped, the NullPointerException goes away. But then the css file does not work properly because it references other files in my project.

如果我将 css 文件放在转储 .class 文件的构建文件夹中,NullPointerException 就会消失。但是随后 css 文件无法正常工作,因为它引用了我项目中的其他文件。

回答by nikolaos

put your yourname.cssfile directly under srcdirectory.

将您的yourname.css文件直接放在src目录下。

scene.getStylesheets().add("yourname.css")

clean and build required

需要清理和构建

回答by Korki Korkig

I think your're missing the slashes which causes that the CSS file can't be found. Try to correct your path reference.

我认为您缺少导致无法找到 CSS 文件的斜杠。尝试更正您的路径参考。

For example:

例如:

-root
--app/Main.java
--assets/double_slider.css

would be:

将是:

String css = this.getClass().getResource("/assets/double_slider.css").toExternalForm();

回答by JBR

I had the same problem. I use NetBeans 7.3 and JavaFX 2.2.7, JDK 7.0_21 on Win7.

我有同样的问题。我在 Win7 上使用 NetBeans 7.3 和 JavaFX 2.2.7、JDK 7.0_21。

My solution was to place the .css in the SAMEfolder as my Java file containing void start(Stage stage). So the Project view looks like this:

我的解决方案是将 .css 放在与包含void start(Stage stage) 的Java 文件相同的文件夹中。所以项目视图看起来像这样:

  • ProjectName
    • Source Packages
      • pkgWhatever
        • Main.java
        • MyCssFile.css
  • 项目名
    • 源包
      • pkgwhatever
        • 主程序
        • MyCssFile.css

(So the CSS file is INthe package, which I find really weird and contraintuitive. Some doc told me to put it in the root of the project so it could be found at runtime, but that didn't work for me in NB. My app now runs regardless of whether I start the file containing "start(..)" by hitting Ctrl+Uor clicking Run on the project's context menu. And it doesn't matter whether I let NB put everything into a JAR or not.)

(所以CSS文件是IN的包,我觉得这真的很奇怪,contraintuitive。有些DOC告诉我把它放在项目的根目录,因此它可以在运行时被发现,但没有在NB适合我的工作。现在,无论我是否通过在项目的上下文菜单上点击+或单击运行来启动包含“ start(..)”的文件,我的应用程序都会运行。我是否让 NB 将所有内容放入 JAR 中都没有关系。)CtrlU

Here's the code that loads the CSS in the above situation:

这是在上述情况下加载 CSS 的代码:

  URL url = this.getClass().getResource("controlStyle1.css");
    if (url == null) {
        System.out.println("Resource not found. Aborting.");
        System.exit(-1);
    }
    String css = url.toExternalForm(); 
    scene.getStylesheets().add(css);

Whereas this didn't work:

而这不起作用:

    scene.getStylesheets().add("controlStyle1.css");

Hope this helps.

希望这可以帮助。

回答by fanatek

I had the same problem (in NetBeans 8). I found a solution here : https://blog.idrsolutions.com/2014/04/use-external-css-files-javafx/

我遇到了同样的问题(在 NetBeans 8 中)。我在这里找到了一个解决方案:https: //blog.idrsolutions.com/2014/04/use-external-css-files-javafx/

My resource file spreadsheet.css was here :

我的资源文件电子表格.css 在这里:

MyApp
-resources
--spreadsheet.css
-source packages
--fr.ccc.myapp.view
---mainView.java
---FXMLMain.fxml

In mainView.java :

在 mainView.java 中:

File f = new File("resources/spreadsheet.css");
spreadsheet.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\", "/"));

Hope this helps.

希望这可以帮助。

回答by user5857068

You can add your style.css directly in your .fxml file as an attribute to your root element as this stylesheets="@your_relative_path/style.css".

您可以将 style.css 直接添加到 .fxml 文件中,作为根元素的属性,如 stylesheets="@your_relative_path/style.css"。

You can use @../style.css if you want to access the css file that is in your src folder

如果要访问 src 文件夹中的 css 文件,可以使用 @../style.css

回答by Malith Ileperuma

I made a small login example and this is how i link my styleshet.css

我做了一个小的登录示例,这就是我如何链接我的 styleshet.css

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("LoginUI.fxml"));


    Scene scene = new Scene(root);

    scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());

    stage.setScene(scene);
    stage.show();

}

You can use this code line to link your css file. but the css file should be in your package.

您可以使用此代码行链接您的 css 文件。但 css 文件应该在你的包中。

scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());

scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());

回答by ayoub soso

Considering your old code:

考虑您的旧代码:

String css =this.getClass().getResource("double_slider.css").toExternalForm(); 
scene.getStylesheets().add(css);

Try changing it to this new code and it will work:

尝试将其更改为这个新代码,它将起作用:

screen.getStylesheets().add(getClass().getResource("double_slider.css").toExternalForm());

When you use getClass(), you don't need to use thiskeyword.

使用时getClass(),不需要使用this关键字。

I hope this work for you. :)

我希望这对你有用。:)

回答by user5450677

in your file .javause this

在你的文件.java 中使用这个

Application.setUserAgentStylesheet(getClass().getResource("application.css")
                .toExternalForm());

回答by manudroid19

It's pretty much simple.

这非常简单。

this.scene.setUserAgentStylesheet(/resources/blabla.css);

This is what worked for me-

这对我有用-

回答by Uluk Biy

Hmmm, are you on Netbeans? Try to "Clean and Build" the project.

嗯,你在 Netbeans 上吗?尝试“清理并构建”项目。