将 JPG 加载到 Swing 应用程序中 [英] Loading JPGs into Swing Apps

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

问题描述

我想我在这里遭受了一些类路径忧郁症.我一直在关注 examples.我阅读了教程.但似乎没有什么对我有用.

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:UsersMyUsersomeIcon.jpeg

Let's say I have a JPEG at the following URI: C:UsersMyUsersomeIcon.jpeg

假设我有一个带有 JPanel 的 Swing 应用程序,我想将此 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).

SO 如何做到这一点?我没有收到任何错误或异常,只是没有看到 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:UsersMyUsersomeIcon.jpeg";
URL imageUrl = getClass().getResource(someIconUri);
ImageIcon imageIcon = new ImageIcon(imageUrl);
myPanel.add(imageIcon);

请指教……我从今天中午开始就一直在处理这个问题……现在已经是午夜了.提前感谢您对正确方向的任何推动.

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:UsersMyUsersomeIcon.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!

这可能是……

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

但是 getResource() 既不需要也没有用,因为它只会在运行时类路径上定位资源.只需直接从 String 构建一个 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.

这些资源可能由框架图标、按钮和菜单图标(Action 图标)、选项卡图标等组成.帮助文件(和相关图片)、启动图片..

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(最常见)并添加到应用程序的运行时类路径(使用清单或其他方式,如小程序元素或 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.它将返回一个特定的现有 File 或更多(取决于如何配置和使用).

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).

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

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

大多数从资源中获取输入的方法(值得一提)将接受 File URL 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 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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