今天在CSDN看见一个帖子:
发现他犯了很多错误,applet中参数与html设置都不一样:而且html设置
的格式完全都错了。
原文为:
import java.applet.*;
import java.awt.*;
public class Appletparam extends Applet
{
private String name;
private String sex;
private int num;
private int age;
public void init()
{
setBackground(Color.gray);
name=this.getParameter("myName:");
sex=this.getParameter("mySex");
num=Integer.parseInt(this.getParameter("myNum"));
age=Integer.parseInt(this.getParameter("myAge"));
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("my name is :"+name, 10, 20);
g.drawString("my sex is :"+sex, 10, 40);
g.drawString("my num is :"+num, 10,60);
g.drawString("my age is :"+age, 10, 80);
}
}
|
Html代码如下:
<html>
<body>
<Applet code="Appletparam.class" height=400 width=400>
<param name=myName value="kaka">
<param sex=mySex value="mail">
<param num=myNum value=200630170>
<param age=myAge value=22>
</Applet>
</body>
</html>
|
执行的错误为:警告: 标记需要名称属性。警告: 标记需要名称属性。警告: 标记需要名称属性。java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:415) at java.lang.Integer.parseInt(Integer.java:497) at Appletparam.init(Appletparam.java:14) at sun.applet.AppletPanel.run(AppletPanel.java:424) at java.lang.Thread.run(Thread.java:619)很显然,html中格式错误了.但是你仔细看之后发现:
name=this.getParameter("myName:");//myname后面的冒号:晕死了 太不小心了~
所以更改之后的正确代码如下:
import java.applet.*;
import java.awt.*;
public class Appletparam extends Applet
{
private String name;
private String sex;
private int num;
private int age;
public void init()
{
setBackground(Color.gray);
name=getParameter("myName");
sex=getParameter("mySex");
num=Integer.parseInt(getParameter("myNum"));
age=Integer.parseInt(getParameter("myAge"));
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("my name is :"+name, 10, 20);
g.drawString("my sex is :"+sex, 10, 40);
g.drawString("my num is :"+num, 10,60);
g.drawString("my age is :"+age, 10, 80);
}
}
|
正确的html代码如下:
<html>
<body>
<Applet code="Appletparam.class" height=400 width=400>
<param name=myName value="kaka">
<param name=mySex value="mail">
<param name=myNum value=200630170>
<param name=myAge value=22>
</Applet>
</body>
</html>
|
阅读(4638) | 评论(2) | 转发(0) |