java 中html文件和applet小程序查看器显示不一样
源文件:importjava.applet.*;importjava.awt.*;classFamily{staticStringfamilyname;Stringnam...
源文件:import java.applet.*;
import java.awt.*;
class Family
{
static String familyname;
String name;
int age;
} public class Example4_4 extends Applet
{
Family father ,son1,son2;
public void init()
{
father=new Family();
son1=new Family(); son2=new Family();
Family.familyname="打"; father.name="鬼子";
son1.name="汉奸"; son2.name="恶霸";
}
public void paint(Graphics g)
{
g.drawString("father: " +father.familyname+father.name,5,10);
g.drawString("son1: " +son1.familyname+son1.name,5,30);
g.drawString("son2: " +son2.familyname+son2.name,5,50);
Family.familyname="杀";
g.drawString("father: " +father.familyname+father.name,5,70);
g.drawString("son1: " +son1.familyname+son1.name,5,90);
g.drawString("son2: " +son2.familyname+son2.name,5,110);
}
}
用APPLETVIEWER EXAMPLE4_4.HTML 显示结果是正确的,但用HTML文本显示就是错误的
html 文件打开后 显示中 Family.familyname全部是“杀”
求高手给解释下下 展开
import java.awt.*;
class Family
{
static String familyname;
String name;
int age;
} public class Example4_4 extends Applet
{
Family father ,son1,son2;
public void init()
{
father=new Family();
son1=new Family(); son2=new Family();
Family.familyname="打"; father.name="鬼子";
son1.name="汉奸"; son2.name="恶霸";
}
public void paint(Graphics g)
{
g.drawString("father: " +father.familyname+father.name,5,10);
g.drawString("son1: " +son1.familyname+son1.name,5,30);
g.drawString("son2: " +son2.familyname+son2.name,5,50);
Family.familyname="杀";
g.drawString("father: " +father.familyname+father.name,5,70);
g.drawString("son1: " +son1.familyname+son1.name,5,90);
g.drawString("son2: " +son2.familyname+son2.name,5,110);
}
}
用APPLETVIEWER EXAMPLE4_4.HTML 显示结果是正确的,但用HTML文本显示就是错误的
html 文件打开后 显示中 Family.familyname全部是“杀”
求高手给解释下下 展开
1个回答
展开全部
我也是个初学者,一下理解仅供参考:不知道你有没有发现,其实你的html文件打开时,一开始Family.familyname是“槐肢打”,但是一下子就变成了“杀”,所以我猜想程序是执行了多次paint方法,所以我在Family中定义了count的静态int变旦明唯量,一开始设为0,然后就在paint方法中加了Family.count++,最后用drawString现实了count的值先是1,然后变成了2,而且你不关闭html或者APPLETVIEWER ,看别的窗口模培,再激活本窗口时,count又会增加。我觉得可能先执行paint了一次,然后执行init,又执行了paint,你激活其他窗口再激活本窗口时,又执行了paint,因此会有这个结果吧。
回答完毕。
回答完毕。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询