简体   繁体   English

背景与 getResourceAsStream(“path”)

[英]Background with getResourceAsStream(“path”)

I am having a folder structure like this.我有这样的文件夹结构。

Project/src/folder/folder/folder/xyz.java
       /img/background.jpg

And I want to put the background.jpg into a JPanel in xyz.java .我想把 background.jpg 放到xyz.java的 JPanel 中。 Just for the Background.只是为了背景。 And I've done it with我已经做到了

    private ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = getClass().getResource(path);

    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

But the problem here is that the image has to be in the same folder than the xyz.java I searched a bit and found that this could be solved with getResourceAsStream("path") but I can't bring up enough knowlege to solve this.但这里的问题是图像必须与xyz.java位于同一文件夹中,我搜索了一下,发现这可以用getResourceAsStream("path")解决,但我无法提供足够的知识来解决这个问题. This is important for me because i want to run my programm as a *.jar file on different operating systems.这对我很重要,因为我想在不同的操作系统上将我的程序作为 *.jar 文件运行。

My problem is that i don't understand how getRessourceAsStream("path") should work different.我的问题是我不明白 getRessourceAsStream("path") 应该如何工作。 I found no tutorial which fits for me.我没有找到适合我的教程。 It seems like the most people use it for different.jar files or Applets.似乎大多数人将它用于不同的.jar 文件或小程序。 And I don't know what a Stream can do, where getRessourceAsStream("path") starts (means how do i have to write the path).而且我不知道 Stream 可以做什么, getRessourceAsStream("path") 从哪里开始(意味着我必须如何编写路径)。 There are so much lacks of knowlage...知识太少了。。。

Edit: I found a solution where I don't have to give a total path.编辑:我找到了一个不需要给出总路径的解决方案。 img = ImageIO.read(getClass().getResource("../../../../img/bg_1.jpg")); I didn't tested it, but it should also run on the other systems.我没有测试它,但它也应该在其他系统上运行。 But the Problem is, that i can only start my program in eclipse.但问题是,我只能在 eclipse 中启动我的程序。 When I export it into a *.jar File, it happens fine but It doesn't load the picture.当我将它导出到 *.jar 文件时,它发生得很好,但它不会加载图片。 And it shrinks the window to a minimum even though I've setPreferredSize.即使我设置了 PreferredSize,它也会将 window 缩小到最低限度。 So i should maybe work the Stream solution out.所以我也许应该解决 Stream 解决方案。 But now it is tome for bed.但现在该睡觉了。

Edit: The relative path works fine when I put the images into a package.编辑:当我将图像放入 package 时,相对路径工作正常。 Not what I really wanted, but after 6h guessing about this tiny problem I had to bring some fishys to swim through my ocean...不是我真正想要的,但在猜测这个小问题 6 小时后,我不得不带一些鱼在我的海洋中游泳......

Since you haven't replied to my comment, I'll ask some questions and make suggestions as an answer:由于您尚未回复我的评论,因此我将提出一些问题并提出建议作为答案:

Is all enclosed in a Jar file?是否全部包含在 Jar 文件中? Have you tried passing the path to the image or the image directory on the command line and read it in as a command line parameter?您是否尝试过在命令行上传递图像或图像目录的路径并将其作为命令行参数读入? And again, why are you using ImageIcons when you're displaying an Image not an ImageIcon?再说一次,为什么在显示图像而不是 ImageIcon 时使用 ImageIcons?

Edit 1编辑 1
regarding your comments:关于您的评论:

I am using an ImageIcon because ##java on freenode.net told me it fits for my habits.我使用 ImageIcon 是因为 freenode.net 上的 ##java 告诉我它适合我的习惯。

I have no idea of who ##java is nor what "habits" you are fitting, but the current fact of the situation is you are using an Image and an ImageIcon has nothing to do with this, so really, get rid of it.我不知道##java 是谁,也不知道你适合什么“习惯”,但目前的情况是你使用的是 Image 而 ImageIcon 与此无关,所以真的,摆脱它。 Simplify and only use what you need.简化并仅使用您需要的东西。

My First solution was something like that: img = ImageIO.read(new File("path"));我的第一个解决方案是这样的: img = ImageIO.read(new File("path")); But it needs also the full path and not some URL path which I know from html.但它还需要完整路径,而不是我从 html 知道的一些 URL 路径。

The path requirements for ImageIO.read are no different from what you're trying to do. ImageIO.read 的路径要求与您尝试做的没有什么不同。 It is a fine addition to your solution.这是您解决方案的一个很好的补充。

The Jar file sould run without any extras on different OS. Jar 文件可以在不同操作系统上运行而无需任何额外内容。 I just need one of it.我只需要其中之一。

You still haven't told us if your images are in a jar or not, and where the images are in relation to your class files, for this is the key to using resources -- loading the resources relative to the location of the class files.您还没有告诉我们您的图像是否在 jar 中,以及图像与您的 class 文件相关的位置,因为这是使用资源的关键——加载相对于 ZA2F2ED4F8EBC2CAB61Z 文件位置的资源.

Loading Images Using getResource 使用 getResource 加载图像

Using an ImageIcon on a JLabel is great when you display the image at its actual size.当您以实际大小显示图像时,在 JLabel 上使用 ImageIcon 非常有用。

If you need to (dynamically) scale the image or anything (like that) then you need to do custom painting with the image.如果您需要(动态)缩放图像或任何东西(类似),那么您需要对图像进行自定义绘画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM