JavaFX 2-无法导入.css文件 [英] JavaFX 2 - Can´t import .css File

查看:157
本文介绍了JavaFX 2-无法导入.css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试进入JavaFX 2.0,但是我无法获得.css样式表在我的应用程序中工作.我在 http://docs.oracle.com/上遵循了该指南. javafx/2/get_started/css.htm#BABBGJBI ,但是每当我尝试通过

I´m currentl trying to get into JavaFX 2.0 but I can´t get .css Stylesheets to work within my application. I´ve followed the guide at http://docs.oracle.com/javafx/2/get_started/css.htm#BABBGJBI but whenever I try to import a .css file via

        scene.getStylesheets().add(Login.class.getResource("loginform.css").toExternalForm());

我收到以下错误消息:

    Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:399)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at Login.start(Login.java:68)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:315)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:174)
    at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
    ... 1 more

css路径位于我的Eclipse项目路径中: C:\Users\UserName\Dropbox\Documents\Eclipse\FirstExamples\loginform.css

The css path is located in my eclipse project path: C:\Users\UserName\Dropbox\Documents\Eclipse\FirstExamples\loginform.css

我将不胜感激,无论使用绝对路径还是示例中的相对路径,都无法正常工作.

I´d appreciate any help, I can´t get it working, not with absolute paths nor with relative ones like shown in the example.

推荐答案

检查eclipse项目的编译输出路径,并确保loginform.css与Login.class位于同一目录

Check the compiled output path of your eclipse project and ensure that loginform.css is in the same directory as Login.class

我通过将样式表放到我的src文件夹中来解决了这个问题, 一个非常好的解决方案,但是通过../formm.css进行升级似乎无效 不知道为什么

I´ve solved the problem by putting stylesheets into my src folder not a very nice solution but going up by ../formm.css doesn´t seem to work dunno why

您正在通过

You are requesting the css file via the class.getResource method, which will look for the resource in the same location the class is located. When you put the stylesheet in your src folder, then whatever build system you are using is copying the stylesheet to the same directory as Login.class during the build. When you don't place it in that src folder, then the copy is not occuring.

如果您不想将css文件放置在与Java源代码相同的目录中,则可以将构建系统设置为从另一个目录复制它,或者在添加样式表时不使用类加载器机制,例如使用基于 user.dir系统的绝对文件或http url或文件url属性(当前工作目录).

If you don't want to place the css file in the same directory as your Java source, then either set your build system up to copy it across from another directory OR don't use the classloader mechanism when you add a stylesheet, e.g. use an absolute file or http url or a file url based on the user.dir system property (current working directory) instead.

尽管如此,只需将样式表放置在src文件夹中并以您正在使用的方式进行访问就可以了,而且在将应用程序部署到不同环境中时,这也使资源定位变得容易.

Really though, just placing leaving the stylesheet in the src folder and accessing it in the manner you are doing is fine and it also makes resource location easy when you deploy the app to different environments.

这篇关于JavaFX 2-无法导入.css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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