将JPG加载到Swing Apps中 [英] Loading JPGs into Swing Apps

查看:115
本文介绍了将JPG加载到Swing Apps中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在这里遭受了一些阶级忧郁。我一直在关注示例。我阅读了教程。但似乎没有什么对我有用。

I think I'm suffering from some classpath blues here. I've been following the examples. I read the tutorials. But nothing seems to be working for me.

假设我在以下URI上有一个JPEG: C:\ Users \ Myyserser \\ someIcon.jpeg

Let's say I have a JPEG at the following URI: C:\Users\MyUser\someIcon.jpeg

让我说我有一个Swing应用程序,其 JPanel ,我想将这个JPEG添加到JPanel中(定位/布局无关紧要)。

And let's say I have a Swing app that has a JPanel, and I want to add this JPEG into the JPanel (positioning/layout doesn't matter).

如何做到这一点?我没有收到任何错误或异常,只是没有看到JPEG加载。我试图使用的代码如下:

How would SO accomplish this? I'm not getting any errors or exceptions, just not seeing the JPEG load. The code I'm trying to use is this:

String someIconUri = "C:\Users\MyUser\someIcon.jpeg";
URL imageUrl = getClass().getResource(someIconUri);
ImageIcon imageIcon = new ImageIcon(imageUrl);
myPanel.add(imageIcon);

请告知...我今天中午以来一直在处理这个......以及它的午夜。提前感谢任何 nudges 正确的方向。

Please advise...I've been dealing with this since noon today...and its midnight. Thanks in advance for any nudges in the right direction.

推荐答案

String someIconUri = "C:\Users\MyUser\someIcon.jpeg";

不,它不是'某个图标URI',它根本不是有效的URI。想想看,它甚至不是一个可编辑的声明,因为 String 包含非法的转义字符!

No it is not 'some icon URI', it is not a valid URI at all. Come to think of it, it is not even a compilable statement, since the String contains illegal escape chars!

这可能是......

It might be something along the lines of..

String someIconUri = "file:///C:/Users/MyUser/someIcon.jpeg";

但不需要 getResource()或者有用,因为它只会在运行时类路径上找到资源。只需直接从字符串构建一个 URL

But then getResource() is neither needed or useful, since it will only locate resources on the run-time class-path. Just construct an URL directly from the String.

但这实际上只是答案的一部分。这就是原因。

But that is really only 'part of an answer'. Here's why.

在这种情况下,基本上有两种类型的资源。应用程序资源和(为了更好的词)用户资源。

In this context, there are basically two types of resources. Application resources and (for want of a better word) User resources.

这些资源可能包括框架图标,按钮和菜单图标(动作图标),标签图标等。帮助文件(和相关图像),启动图像..

These resources might consist of things like frame icons, button and menu icons (Action icons), icons for tabs. Help files (and associated images), splash images..

它们应该添加到一个单独的Jar(最常见)并添加到运行时类路径应用程序(使用清单或其他方式,如applet元素或JNLP文件)。

They should be added to a separate Jar (most often) and added to the run-time class-path of the application (either using the manifest or other means like applet element or JNLP file).

应该通过URL访问应用程序资源,可以使用 getResource()获取:

Application resources should be accessed by URL, which can be obtained using getResource():

URL iconUrl = this.getClass().getResource("/icons/copy.jpg");



用户资源。



用户想要打开(编辑/打印)现有文本文档,从图像帧制作动画GIF,在其文件系统上编辑图像..

User resources.

The user wants to open (edit/print) an existing text document, make an animated GIF from image frames, edit an image on their file system..

对于这些类型的资源(并假设应用程序是受信任的或没有安全管理器),为用户提供 JFileChooser 。它将返回一个特定的,现有的文件或更多(取决于配置和使用的方式)。

For these types of resources (and presuming the app. is trusted or has no security manager), offer the user a JFileChooser. It will return a specific, existing File or more (depending on how configured and used).

在这种情况下,永远不要将文件转换为其他任何东西,只需直接使用实例即可。

In that case, never convert the File to anything else, just use the instance(s) directly.

从资源中获取输入的大多数方法(值得一提)将接受文件 网址 InputStream

Most methods that take input from resources (worth mentioning), will accept a File URL or InputStream.

最后一个对内存中生成的内容很有用,或者从套接字或JNLP API等源代码中获取 FileContents 对象。后者对沙盒应用程序特别感兴趣。使用 Java Web Start 启动或嵌入。

The last is useful for something generated in memory, or obtained from sources such as sockets or a JNLP API FileContents object. The latter is of special interest to sand-boxed apps. launched or embedded using Java Web Start.

这篇关于将JPG加载到Swing Apps中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆