在图像的不同文件夹中使用链接(Java)

时间:2011-12-21 22:36:31

标签: java image swing hyperlink

我正在设计一个带有自定义按钮的计算器。当然,我想将我的图像文件组织在与包接口不同的文件夹中。 文件夹的位置是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

谢谢!

2 个答案:

答案 0 :(得分:2)

"/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。基本上,你必须弄清楚你的代码在哪个文件夹下运行,也称为“工作目录”,并从那里构建一个相对路径。

答案 1 :(得分:2)

只需删除第一个正斜杠。

seven = new ButtonImage("interfaces/Seven/seven-normal.png");

interfaces是与JAR位于同一文件夹中的文件夹。