Java applet Error … What is wrong?

前端 未结 2 1684
闹比i
闹比i 2020-12-06 23:43

Java applet code

package M257Applet

import java.applet.*;

import javax.swing.*;

import java.awt.*;

public class HellowApplet extends JAp         


        
相关标签:
2条回答
  • 2020-12-06 23:56

    Your class is in a package. It's file name should match.

    code="M257Applet/HellowApplet.class"
    

    (It's a good idea to follow conventions. Package names should be all lower case.)

    0 讨论(0)
  • 2020-12-07 00:04

    Problem is with the package. You need to change the code attribute of applet, and based on where you have placed your HTML, the codebase attribute too. You have to place HellowApplet.class in a directory called M257Applet (because that is the package you have given), and the applet tag should look something like:

    <applet code="M257Applet.HellowApplet" ... ></applet>
    

    For this to work, your HTML has to be in the same directory as M257Applet (not inside M257Applet). Alternatively, you can specify the codebase attribute. For eg, with the following directory structure:

    somedir
      +-- hello.html
      +-- M257Applet
      |    +-- HellowApplet.class
    

    the applet will work. If however, you had

    anotherdir
      +-- hello.html
      +-- somedir
      |   +-- M257Applet
      |   |    +-- HellowApplet.class
    

    then you will have to specify codebase attribute like so:

    <applet code="M257Applet.HellowApplet" codebase="somedir" ... ></applet>
    

    So, you should have codebase pointing to the directory containing your package, and code has to have your package name also in it.

    Edit: Please note, even though code="HellowApplet.class" will work, the correct way of specifying the applet is without the ".class" at the end.

    0 讨论(0)
提交回复
热议问题