将小程序字节码嵌入Html文件里,让浏览器执行的问题
如下是一段JAVAAPPLET代码,在Eclipse里编写的,并可以用appletviewer运行。[code=Java]importjava.applet.Applet...
如下是一段JAVA APPLET代码,在Eclipse里编写的,并可以用appletviewer运行。
[code=Java]
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class App15_1 extends Applet implements MouseListener
{
int x1, x2, y1, y2, width, height;
boolean flag = false;
public void init()
{
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
flag = true;
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
if (flag)
{
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
if (x1 > x2 && y1 > y2)
{
x1 = x2;
y1 = y2;
}
else if (x1 > x2 && y1 < y2)
x1 = x2;
else if (x1 < x2 && y1 > y2)
y1 = y2;
g.drawRect(x1, y1, width, height);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}
[/code]
编译后获得该代码的字节码文件"App15_1.class",然后再按照书上编写一段Html代码
[code=HTML]
<html>
<applet>
codebase="."
code="App15_1.class"
height=150
width=300
alt="sorry,wrong"
align="Middle"
vspace="25">
</applet>
</html>
[/code]
将这个Html代码和App15_1.class放置在同一目录里,用IE打开Html文件,显示如图:
[img=http://p13.freep.cn/p.aspx?u=v20_p13_photo_1209271158228049_0.png][/img]
之前还提示“ClassdefNotFound”,点击详细信息出现上面那张图。
JDK安装的是1.7,我确定path和classpath已经配置正确了,为什么不能在浏览器上看到我写的程序? 展开
[code=Java]
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class App15_1 extends Applet implements MouseListener
{
int x1, x2, y1, y2, width, height;
boolean flag = false;
public void init()
{
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
flag = true;
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2 = e.getX();
y2 = e.getY();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
if (flag)
{
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
if (x1 > x2 && y1 > y2)
{
x1 = x2;
y1 = y2;
}
else if (x1 > x2 && y1 < y2)
x1 = x2;
else if (x1 < x2 && y1 > y2)
y1 = y2;
g.drawRect(x1, y1, width, height);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}
[/code]
编译后获得该代码的字节码文件"App15_1.class",然后再按照书上编写一段Html代码
[code=HTML]
<html>
<applet>
codebase="."
code="App15_1.class"
height=150
width=300
alt="sorry,wrong"
align="Middle"
vspace="25">
</applet>
</html>
[/code]
将这个Html代码和App15_1.class放置在同一目录里,用IE打开Html文件,显示如图:
[img=http://p13.freep.cn/p.aspx?u=v20_p13_photo_1209271158228049_0.png][/img]
之前还提示“ClassdefNotFound”,点击详细信息出现上面那张图。
JDK安装的是1.7,我确定path和classpath已经配置正确了,为什么不能在浏览器上看到我写的程序? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询