Applet works in IDE, but not in browser
I have created a simple applet and HTML document, but when I open the HTML document, the applet will not show. Java is enabled, and the code should be correct. but I just get a blank page. The applet runs fine in eclipse. I tried removing the stop and destroy methods which did nothing, not sure that it would anyway, this is my first ever applet code.
I did compile the .java file using the javac command and placed the html document and 开发者_JAVA技巧.class file in the same directory.
When using IE9 it gives me the error: Lamp (wrong name: mondrian/Lamp)
APPLET CODE
package mondrian;
import java.applet.*;
import java.awt.*;
public class Lamp extends Applet {
public void init() {
setBackground(Color.BLACK);
}
public void start() {
}
public void paint (Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 90, 90);
g.fillRect(250, 0, 40, 190);
g.fillRect(80, 110, 100, 20);
}
public void stop() {
}
public void destroy() {
}
}
HTML DOCUMENT
<html>
<body>
<APPLET CODE="Lamp.class" WIDTH=200 HEIGHT=50>
</APPLET>
</body>
</html>
I see that the class is in a package. If you are running the class file place the html one directory below and refer to the class together with its package as in:
<applet code=mondrian.Lamp.class
width=1200 height=1200>
</applet>
if you prefer to run from a jar place the html in the same directory and write
<applet code=mondrian.Lamp.class
archive="myarchive.jar"
width=1200 height=1200>
</applet>
jar is more portable of course than the numerous class files in a directory that needs to carry the package name.
精彩评论