简体   繁体   English

我似乎无法修复的JApplet错误

[英]JApplet error that I can't seem to fix

I am getting an annoying error for my Java applet. 我的Java小程序出现一个令人讨厌的错误。 I am new to applets so please note that I am not that experienced with this at all. 我是applets的新手,所以请注意,我一点都不熟悉。

I have an HTML file labelled as index.html with the following code: 我有一个标记为index.html的HTML文件,其中包含以下代码:

    <HTML>   
    <HEAD>
            <TITLE>Applet JAR Example
        </TITLE>
    </HEAD>  
    <BODY> 
        <CENTER>
            <B>Are YOU ready to dance??
            </B>
            <BR>
                <BR>

                <APPLET CODE="shawn/Main.class" ARCHIVE="lol.jar"
                        WIDTH=400      
                HEIGHT=300>    
</APPLET>   
</CENTER>
</BODY>  
</HTML>

In the same directory, I have a Jar file labelled as lol.jar with the following code: 在同一目录中,我有一个标为lol.jar的Jar文件,其中包含以下代码:

package shawn;

import java.applet.AudioClip;
import java.awt.*;
import java.io.File;
import java.io.Serializable;
import javax.swing.*;

public class Main extends JApplet implements Serializable {

        Image img = Toolkit.getDefaultToolkit().getImage("hey.gif");

        @Override
        public void init(){
            playSound();
        }

       @Override
    public void paint(Graphics g)    {
        g.drawImage(img, 0, 0, this);
    }

    public void playSound(){
        AudioClip ac = getAudioClip(getCodeBase(), "hey.wav");
        ac.play();
    }
}

Inside that same directory, I have two files labelled as hey.wav, and hey.gif. 在同一目录中,我有两个文件分别标记为hey.wav和hey.gif。

When I execute the page the applet fails to load, outputting only the message Error. Click for details 当我执行页面时,小程序无法加载,仅输出Error. Click for details消息Error. Click for details Error. Click for details . Error. Click for details When I click, it says: 当我单击时,它说:

RuntimeException RuntimeException

followed by... 其次是...

java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException

Everything works when I run it in Eclipse, but it only does this when I export it. 当我在Eclipse中运行时,一切正常,但是只有在导出时,它才能运行。 I`ll add more details if needed. 如果需要,我会添加更多详细信息。

One definite problem in the applet is: 小程序中的一个确定的问题是:

Image img = Toolkit.getDefaultToolkit().getImage("hey.gif");

If you look at the JavaDocs for getImage(String) it states: 如果您在JavaDocs中查看getImage(String)它将指出:

Returns an image which gets pixel data from the specified file , whose format can be either GIF, JPEG or PNG. 返回从指定文件中获取像素数据的图像,其格式可以为GIF,JPEG或PNG。 The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. 基础工具箱尝试将多个具有相同文件名的请求解析到相同的返回Image。

The highlight of file was by my choice. 文件的亮点是我选择的。 Applets and files are rarely used together, and it is not appropriate for this situation. 小程序和文件很少一起使用,因此不适用于这种情况。 Instead the Image must be accessed by URL . 相反,必须通过URL访问Image

Applet offers instead Applet.getImage(URL) & getImage(URL,String) . Applet改为提供Applet.getImage(URL)getImage(URL,String) The 2nd is particularly handy when we need to form an URL relative to the code base or document base. 当我们需要形成一个相对于代码库或文档库的URL时,第二个特别方便。

If the image is located in the same directory as the HTML, it might be loaded using something along the lines of: 如果图像与HTML位于同一目录中,则可以使用以下方式加载图像:

Image img = getImage(getDocumentBase(), "hey.gif");

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

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