使用Swing从本地目录获取图像? [英] Get Image from local directory using Swing?
问题描述
我试图将图像从本地目录加载到ImageIcon(URL)中.该图像文件是从.jar文件访问的.jar文件名为swingex.jar.项目结构类似
I trying to load image from local directory into ImageIcon(URL).This images files access from .jar file.The jar file name is swingex.jar.The project structure like
F:/>SwingExample
|
|___src
|
|___build.xml
|
|___lib
|
|___swingsex.jar(generated through build.xml file)
|
|__resource
|
|_____images
|
|___logo1.png
如何读取logo1.png文件?
How to read logo1.png file?
我想喜欢这个
-
file:///f://resources//images//processedimages//
返回null
ClassLoader.getSystemResource("resources/images/processedimages/");
返回null
更新:-我仍然有问题.因为我在SwingExample上创建了jar文件并排除了resource/images目录.当运行jar文件时,它无法识别resource/images文件夹.但是我通过Eclipse运行了SwingExample项目,代码运行正常.代码是
Update :- Still i have problem.because i created jar file on SwingExample and excludes the resource/images directory .When run the jar file its not recognized the resource/images folder.But i ran the SwingExample Project through eclipse it working fine.The code is
File directory = new File (".");
Image img = null;
String path="";
URL url=null;
try {
path=directory.getCanonicalPath()+"/resources/images/logo1.png";
img = ImageIO.read(new File(getDefaultImageUploadPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ImageIcon(img);
推荐答案
URL imgURL =
new File( "F:/SwingExample/resource/images/logo1.png" ).toURI().toURL();
但是我建议将资源放入src.
But I suggest to put resource into src.
如果资源位于src中,则可以通过类加载器轻松访问它们.
If resource is in src, you may access them by class loader easily.
getClassLoader().getResourceAsStream( "resource/images/logo1.png" );
这篇关于使用Swing从本地目录获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!