最近在学习java se ,在applet中遇到问题,自己写的applet和下载别人的applet代码均无法在网页中执行,下面给出源码
// the applet source file
import java.util.*;
import java.awt.*;
import java.applet.Applet;
public class useapplet extends Applet
{
String strTime=new String();
public void init()
{}
public void start()
{
Date d=new Date();
strTime=d.toString();
repaint();
}
public void paint(Graphics g)
{
g.drawString("当前时间为"+strTime,20,30);
}
};// the html file
<html>
<title>useapplet!Applet</title>
<body>
<applet code="useapplet.class" height=200 width=300>
</applet>
</body>
</html>该applet程序可以在dos下以appletviewer命令启动执行,但用IE和360浏览器打开html文件却无法在网页中正常显示
求高手指点
// the applet source file
import java.util.*;
import java.awt.*;
import java.applet.Applet;
public class useapplet extends Applet
{
String strTime=new String();
public void init()
{}
public void start()
{
Date d=new Date();
strTime=d.toString();
repaint();
}
public void paint(Graphics g)
{
g.drawString("当前时间为"+strTime,20,30);
}
};// the html file
<html>
<title>useapplet!Applet</title>
<body>
<applet code="useapplet.class" height=200 width=300>
</applet>
</body>
</html>该applet程序可以在dos下以appletviewer命令启动执行,但用IE和360浏览器打开html文件却无法在网页中正常显示
求高手指点
另外,你那个useapplet.class最好不要放在默认包下,如果建立了非默认包路径,比如p1.XXX.class,那么这个XXX.class要放在p1目录下,p1目录与当前运用applet的.html在同一目录。
applet.html
applet
applet\Applet1.class
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试Java Applet</title>
</head><body>
<applet cabbase="." code="applet.Applet1.class" name="MyApplet1" id="MyApplet1" width="200" height="100">
</applet><script language="javascript"> //在IE下可以直接使用MyApplet1
var map1 = document.getElementById("MyApplet1");
var mes = map1.get("H W");
alert(mes);
mes = map1.getMessage();
alert(mes);</script>
</body>
</html>
package applet;import java.applet.Applet;public class Applet1 extends Applet { /**
*
*/
private static final long serialVersionUID = 1L; public Applet1(){}
private String message = "Hello Applet!" ; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
public static void main(String args[]){
System.out.println("Hello Applet1!");
}
public static String get(String in){
return in;
} @Override
public void init() {
// TODO Auto-generated method stub
super.init();
}
}