【问题标题】:Loading images in Java Web Start applet在 Java Web Start 小程序中加载图像
【发布时间】:2013-09-04 03:28:25
【问题描述】:

在普通小程序中,我使用它来将图像加载到 JLabels 中

    Image back2 = getImage(getDocumentBase(), "images/blank_blue.png");
    ImageIcon background2Icon = new ImageIcon(back2);
    JLabel pic2 = new JLabel(background2Icon);

当我尝试使用 Java Web Start 小程序执行此操作时,什么也没有出现。 在 Java 控制台中,它这样说 basic: Loaded image: file:/C:/Users/jdfksl/Desktop/webStartTest/images/blank_blue.png 而且没有错误,但不会出现。

我在 JLabels 中加载了图像,这些 JLabels 以这种方式在 Java Web Start 中工作

BufferedImage image=null;
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
            image = ImageIO.read(classLoader.getResourceAsStream("images/ghast_skin.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

但是,我想要一种更短的加载图像的方式。 还有其他选择吗?

这是我的 jnlp 文件内容

     <?xml version="1.0" encoding="UTF-8"?>
 <jnlp href="jws_tester.jnlp">
     <information>
       <title>JWS tester</title>
       <vendor>HTZ.</vendor>
       <description>Testing with JWS</description>
       <offline-allowed />
     </information>
     <resources>
       <j2se version="1.4+"
             href="http://java.sun.com/products/autodl/j2se" />
       <jar href="tester.jar" main="true" />
       <!-- Specify if using JNLP extensions
                                <extension name="SomeExtension"
                  href="http://some.server.com/some_extension.jnlp" />
                                -->
     </resources>

     <security>
    <all-permissions/>
</security>

     <applet-desc 
         name="JWS tester"
         main-class="mather.Main"
         width="900"
         height="600">
     </applet-desc>
 </jnlp>

【问题讨论】:

  • JNLP 的内容是什么?是否已使用JaNeLA 进行验证?
  • @AndrewThompson 我运行了 JaNeLA,它用黄色表示 Content type content/unknown does not equal expected type of application/x-java-jnlp-file 和红色 The processing instruction target matching "[xX][mM][lL]" is not allowed。我不知道那是什么意思
  • @AndrewThompson 实际上,从 JaNeLA 的帮助中,它说 It is typical for JNLP files checked from the local file system to be of incorrect type.,所以“内容类型未知”应该没问题
  • “我不知道这意味着什么” JaNeLA 应用中有更多详细信息。 (例如点击错误)。但是,如果你回答了我的第一个问题,我可以告诉你这意味着什么..
  • @AndrewThompson 我发布了我的 JNLP 文件,这是你想要的吗?

标签: java image applet java-web-start


【解决方案1】:

一种方法是将图像添加到您的构建路径并从任何此类访问图像。

new ImageIcon(TestClass.class.getResource("/image.png"));

在这种方法中,图像将被添加到您的 jar 文件中,因此您的 jar 文件可能会变得更大。

【讨论】:

    【解决方案2】:

    试试这个:

    Image image = new ImageIcon("path").getImage();
    

    编辑:这个path 可以是图像的完整路径,也可以是项目文件夹的相对路径。 IE,如果你的图片路径是:

    C:\Users\User\Documents\NetBeansProjects\YourProject\src\resource\image.png
    

    您可以通过以下方式访问它

    src\resource\image.png
    

    【讨论】:

    • 小程序不能以File 的身份访问服务器上的资源,也不能访问嵌入的资源。在这个用例中,这是一种完全不适合加载图像的策略。
    猜你喜欢
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2011-02-03
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多