/Scoping.java // import java.awt.Container; import javax.swing.*;public class Scoping extends JApplet { JTextArea outputArea; int x = 1; //instance variable
public void init () { outputArea = new JTextArea(); Container c =getContentPane(); c.add( outputArea ); }
public void start() { int x = 5; //variable local to method start
outputArea.append ( "local x in start is " + x );
methodA(); methodB(); methodA(); methodB();
outputArea.append ("\n\nlocal x in start is" + x ); }
public void methodA() { int x =25; //initialized each time a is called
outputArea.append (" \n\nlocal x in methodA is"+ x + "after entering methodA"); ++x; outputArea.append (" \nlocal x in methodA is " + x + "before exiting methodA"); }
public void methodB() { outputArea.append("\n\ninstance variable x is " + x + " on entering methodB"); x *=10; outputArea.append ("\ninstance variable x is" + x + "on exiting methodB"); } }
怪了 我试了一下 好好的嘛!!!第一部:把你的源代码另存在一个新的目录下,接着编译出Scoping.class 与Scoping.java放在同一目录下。 第二步:随便找了一个现成的html结构套进去就好了: <html> <head> <title>Simple Graph (1.1)</title> </head> <body> <h1>Simple Graph(1.1)</h1> <hr> <applet code=Scoping.class width=300 height=120> alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is completely ignoring the <APPLET> tag! </applet> <hr> <a href="Scoping.java">The source</a>. </body> </html>好了 另存为 yourname.html 与.class .java文件放在同一目录下。运行 得出结果。第三步:进入dos的当前文件夹目录下 如文件在 C:\WINDOWS\Desktop\new目录下 就在dos下进入C:\WINDOWS\Desktop\new 运行"appletviewer yourname.html" 得出的结果与浏览器打开直接运行完全一样
//
import java.awt.Container;
import javax.swing.*;public class Scoping extends JApplet {
JTextArea outputArea;
int x = 1; //instance variable
public void init ()
{
outputArea = new JTextArea();
Container c =getContentPane();
c.add( outputArea );
}
public void start()
{
int x = 5; //variable local to method start
outputArea.append ( "local x in start is " + x );
methodA();
methodB();
methodA();
methodB();
outputArea.append ("\n\nlocal x in start is" + x );
}
public void methodA()
{
int x =25; //initialized each time a is called
outputArea.append (" \n\nlocal x in methodA is"+ x +
"after entering methodA");
++x;
outputArea.append (" \nlocal x in methodA is " + x +
"before exiting methodA");
}
public void methodB()
{
outputArea.append("\n\ninstance variable x is " + x +
" on entering methodB");
x *=10;
outputArea.append ("\ninstance variable x is"
+ x + "on exiting methodB");
}
}
第二步:随便找了一个现成的html结构套进去就好了:
<html>
<head>
<title>Simple Graph (1.1)</title>
</head>
<body>
<h1>Simple Graph(1.1)</h1>
<hr>
<applet code=Scoping.class width=300 height=120>
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is completely ignoring the <APPLET> tag!
</applet>
<hr>
<a href="Scoping.java">The source</a>.
</body>
</html>好了 另存为 yourname.html 与.class .java文件放在同一目录下。运行
得出结果。第三步:进入dos的当前文件夹目录下 如文件在 C:\WINDOWS\Desktop\new目录下 就在dos下进入C:\WINDOWS\Desktop\new 运行"appletviewer yourname.html" 得出的结果与浏览器打开直接运行完全一样
//<applet code=Scoping width=300 height=120>
//</applet>
在用appletviewer解析你的java文件就成。因为appletviewer是在网页中寻找<applet>标记来解释,你用这个来解析你的java文件,但是文件里面没有这个标记,当然就要报错啊。