1 / 20

第十三章

第十三章. Applet 程序. 13-1 Applet 简介. Applet 程序继承自 java.applet.Applet 类,嵌入 HTML 文档中,通常置于服务器端,下载到本地机后,通过浏览器在用户端执行。 Applet 类提供了 applet 程序与所执行环境间的标准接口,同时还提供了 applet 程序在浏览器上执行的架构,包括 init() , start() , stop() 和 destroy() 4 个方法。. 13-1-2 applet 程序的运行过程.

penny
Download Presentation

第十三章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十三章 Applet 程序

  2. 13-1 Applet简介 Applet程序继承自java.applet.Applet类,嵌入HTML文档中,通常置于服务器端,下载到本地机后,通过浏览器在用户端执行。 Applet类提供了applet程序与所执行环境间的标准接口,同时还提供了applet程序在浏览器上执行的架构,包括init(),start(),stop()和destroy()4个方法。

  3. 13-1-2 applet程序的运行过程 当浏览器装载带有applet程序的网页时,首先为applet及其全程变量分配存储空间,然后运行applet的init()方法,接着调用start()方法,之后调用paint()方法。如果用户离开该网页,使该页成为不活动状态或最小化浏览器窗口,stop()方法被调用。当用户离开applet所在网页一段时间后,又重新回到其所在网页(重新激活该网页)时,再次执行start()方法及paint()方法。当用户真正离开浏览器时先执行stop()方法,再执行destroy()方法。

  4. 13-1-3 applet程序的建立和运行 applet小程序的建立方法与Java程序类似,如首先通过文本编辑器如写字板输入源程序,源程序的扩展名为.java。再通过javac.exe程序对源文件进行编辑,产生扩展名为.class的字节码文件,即类文件。也可以直接在JCreator等集成开发环境中建立、编译并运行applet程序。 applet小程序不能独立运行,必须嵌入到HTML文件(网页)中。在浏览器中加载HTML文件时,才开始执行其中的applet程序。

  5. 13-1-3 applet程序的建立和运行 内嵌有小程序的HTML文件的基本组成: <HTML> <APPLET CODE=“applet类名.class” WIDTH=窗口宽度 HEIGHT=窗口高度> </APPLET> </HTML> 其中appletl类名代表要嵌入的applet字节码文件名,窗口宽度和高度分别表示applet程序运行时窗口的初始尺寸。窗口尺寸以像素为单位。

  6. 13-1-3 applet程序的建立和运行 1.利用浏览器运行applet程序 2.利用appletviewer运行applet程序 3.利用JCreator建立和运行applet程序

  7. 13-2 applet程序举例 例:基本的applet程序 import java.awt.Graphics; import javax.swing.JApplet; public class SimpleApplet extends JApplet { public void paint(Graphics g) { g.drawString("Hello applet!",50,60); } }

  8. 将该源文件编译,生成字节码文件SimpleApplet.class。再建立如下的HTML文件SimpleApplet.html。将该源文件编译,生成字节码文件SimpleApplet.class。再建立如下的HTML文件SimpleApplet.html。 <HTML> <APPLET CODE="SimpleApplet.class" WIDTH=250 HEIGHT=100> </APPLET> </HTML> 在DOS命令行输入以下命令: appletviewer SimpleApplet.html

  9. SimpleApplet.html运行结果

  10. 例:绘制图形 applet源程序文件DrawingApplet.java的内容如下: import java.awt.Graphics; import javax.swing.JApplet; public class DrawingApplet extends JApplet { public void paint(Graphics g) { g.drawLine(40,30, 200,30); g.drawRect(40,50,160,150); g.drawOval(45,55,150,140); g.drawLine(40,220, 200,220); g.drawString("Drawing!",100,130); } }

  11. 将该源文件编译,生成字节码文件DrawingApplet.class。再建立如下的HTML文件DrawingApplet.html:将该源文件编译,生成字节码文件DrawingApplet.class。再建立如下的HTML文件DrawingApplet.html: <HTML> <APPLET CODE="DrawingApplet.class" WIDTH=250 HEIGHT=300> </APPLET> </HTML> 在DOS命令行输入以下命令: appletviewer DrawingApplet.html

  12. DrawingApplet.html的运行结果

  13. 例:覆盖init()方法的applet程序 import java.awt.Graphics; import javax.swing.*; public class ComputationApplet extends JApplet { int n ; long s=1; public void init() { String nStr=JOptionPane.showInputDialog( "输入一个正整数");

  14. n = Integer.parseInt(nStr); for (int i=1; i<=n; i++) s = s*i; } public void paint(Graphics g) { g.drawRect(40,30, 150,55); g.drawString(n + " != " + s, 60,50); } }

  15. 将该源文件编译,生成字节码文件ComputationApplet.class。再建立如下的HTML文件ComputationApplet.html:将该源文件编译,生成字节码文件ComputationApplet.class。再建立如下的HTML文件ComputationApplet.html: <HTML> <APPLET CODE="ComputationApplet.class" WIDTH=250 HEIGHT=100> </APPLET> </HTML>

  16. 运行init方法产生的窗口 运行paint方法产生的窗口

  17. 例: Applet类各方法功能展现 import java.awt.Graphics; import javax.swing.JApplet; public class HelloApplet extends JApplet { int initCount=0, stopCount=0, startCount=0, paintCount=0, destroyCount=0; public void init() { initCount++; } public void stop() { stopCount++; } public void start() { startCount++; } public void destroy() { destroyCount++; }

  18. public void paint(Graphics g) { paintCount++; String outString="init: "+initCount+ " start:"+startCount+ " paint:"+ paintCount+ " stop:"+stopCount+ " destroy:"+destroyCount; g.drawString(outString,50,50); } }

  19. 将该源文件编译,生成字节码文件HelloApplet.class。再建立如下的HTML文件HelloApplet.html:将该源文件编译,生成字节码文件HelloApplet.class。再建立如下的HTML文件HelloApplet.html: <HTML> <!--HelloApplet.html--> <APPLET CODE="HelloApplet.class" WIDTH=400 HEIGHT=100> </APPLET> </HTML>

  20. 初始窗口 改变窗口大小后 最小化窗口后 再启动applet

More Related