Java小程序代码

package M257Applet

import java.applet.*;

import javax.swing.*;

import java.awt.*;

public class HellowApplet extends JApplet {

    public  void init(){

        Container cp = getContentPane();
        JLabel lb = new JLabel("Hellowwwww");
        cp.add(lb);
    }

}

html文件
<html>
<head>
<title>Applet</title>
</head>
<body>
<APPLET CODE = HellowApplet.class  WIDTH =  400   HEIGHT = 400 >
</APPLET>
</body>
</html>

错误
Java Plug-in 1.6.0_22
Using JRE version 1.6.0_22-b04 Java HotSpot(TM) Client VM
User home directory = C:\Users\pc
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------


java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: HellowApplet (wrong name: M257Applet/HellowApplet)

最佳答案

包裹有问题。您需要更改applet的code属性,并且根据您放置HTML的位置,还需要更改codebase属性。必须将HellowApplet.class放在名为M257Applet的目录中(因为这是您提供的包),applet标记应该如下所示:

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

要使其工作,HTML必须与M257Applet位于同一目录中(不在M257Applet中)。或者,可以指定codebase属性。对于eg,使用以下目录结构:
somedir
  +-- hello.html
  +-- M257Applet
  |    +-- HellowApplet.class

小程序将工作。但是,如果你
anotherdir
  +-- hello.html
  +-- somedir
  |   +-- M257Applet
  |   |    +-- HellowApplet.class

然后,您必须指定如下代码基属性:
<applet code="M257Applet.HellowApplet" codebase="somedir" ... ></applet>

因此,您应该让codebase指向包含您的包的目录,并且code还必须在其中包含您的包名称。
编辑:
请注意,即使code="HellowApplet.class"可以工作,但正确的小程序指定方式是在末尾没有“.class”。

10-06 09:15