【发布时间】:2021-07-28 03:30:05
【问题描述】:
我尝试将图像放到 JButton 上。写下图片的路径后,运行程序,然后图片不在按钮上。代码示例如下:
public static JButton start = new JButton(new ImageIcon("resources//img//menu//Start_Game.png"));
“resources”文件夹位于“src”文件夹中。 Here is the Hirarchy in Eclipse
在我仔细检查后,我拼写正确,我换了一条路。
public static JButton start = new JButton(new ImageIcon(System.getProperty("user.home") + "//Tindir//Hauptmenue//Start_Game.png"));
它突然起作用了。很明显,第一条路径不正确。
问题是否可以在与代码相同的项目文件夹中输入图像的路径?
【问题讨论】:
-
你将资源文件夹放在顶层项目层下,并使用“/menu/item.png”或“/game/item.png”分别访问菜单或游戏子文件夹中的图像。
-
应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。
标签: java image swing icons embedded-resource