InvocativeTargetException尝试查找.css文件时 [英] InvocativeTargetException while trying to find .css file

查看:91
本文介绍了InvocativeTargetException尝试查找.css文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了从StackOverFlow的五个不同的解决方案,没有一个工作。我有一个CSS文件,我试图应用于我的GUI应用程序通过调用:

I've tried around five different solutions from StackOverFlow and none of them work. I have a CSS file that I'm trying to apply to my GUI application by calling:

scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

我的文件系统如下:

myProject

myProject

------- src

------- src

--- ----------- style.css

-------------- style.css

-------------- package1

-------------- package1

--------------------- GUI.java //< ----需要style.css的类

--------------------- GUI.java // <---- Class which needs style.css

-------------- package2

-------------- package2

------- bin

------- bin

我已经尝试过这么多的解决方案。我实际上有这个问题之前,它会看到我的项目/ bin /文件。这是痛苦的沮丧。我不想严格定义位置,因为如果文件移动或去不同的计算机,它将不再工作。

I've tried so many solutions already. I actually had this problem before where it would look in myProject/bin/ for the File. It's painfully frustrating. I don't want to strictly define the location because if the file ever moves or goes to a different computer, it would no longer work.

推荐答案

假设一切都正确部署,所以你在bin文件夹中看到style.css,你应该能够做

Assuming everything is deployed correctly, so you see style.css in the bin folder, you should be able to do either

scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());

(注意前导斜杠 / )或

scene.getStylesheets().add(getClass().getClassLoader().getResource("style.css").toExternalForm());

这篇关于InvocativeTargetException尝试查找.css文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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