我正在设计一个带有自定义按钮的计算器。自然,我想将图像文件组织在与程序包界面不同的文件夹中。
文件夹的位置是interfaces / Seven / seven-normal.png,但是每当我不包含完整链接时
"C:\Liloka\Source\interfaces\Seven\seven-normal.png"
它不起作用,一切都消失了。我发誓我已经看到这是用普通代码完成的。如果我在适当的程序中使用了此代码,那么我就不会期望人们将链接更改为放置代码的位置!这是我一直在使用的代码:
seven = new ButtonImage("/Seven/seven-normal.png"); - Doesn't work
nine = new ButtonImage("C:/Users/Liloka/workspace/WebsiteContent/src/interfaces/Nine/nine-normal.png"); - Does work
谢谢!
最佳答案
"/Seven/seven-normal.png"
...是通往
C:\Seven\seven-normal.png
的路径-因为/
在路径的开始,这实际上意味着from the root of the drive, go to the "Seven" folder, and then load "seven-normal.png"
您必须使用相对路径,例如
"../../interfaces/Seven/seven-normal.png"
或仅"interfaces/Seven/seven-normal.png"
第一个路径将带您“上”两个文件夹,然后下到
interfaces/Seven/seven-normal.png
。本质上,您必须找出代码在哪个文件夹下运行(也称为“工作目录”),并从此处构造相对路径。