Java Applet就是用Java語言編寫的小應用程式,它們可以直接嵌入到網頁中,並且能夠產生特殊的效果
applet經編譯後,會產生.class的檔案,把.class的檔案嵌在html的網頁中,只要使用者連到一個網頁裡,applet便會隨著網頁下載到使用者的電腦運行
applet類別的繼承關係如下:
java.lang.Object
# java.awt.Component
# java.awt.Container
java.awt.Panel
java.applet。 #applet的
生命週期
#applet的四個主要方法
import java.awt.*; import java.util.*; public class cam1 extends java.applet.Applet { String s; int inits=0,starts=0,stops=0; public void init() { inits++; showStatus("now init"); System.out.println("now init"); pause(); showStatus("leave init"); System.out.println("leave init"); pause(); } public void start() { starts++; showStatus("now start"); System.out.println("now start"); pause(); showStatus("leave start"); System.out.println("leave start"); pause(); } public void stop() { stops++; showStatus("now stop"); System.out.println("now stop"); pause(); showStatus("leave stop"); System.out.println("leave stop"); pause(); } public void paint(Graphics g) { s="inits: "+inits+"starts: "+starts+"stops: "+stops; g.drawString(s, 10, 10); System.out.println("now paint: "+s); pause(); } public void pause() { Date d=new Date(); long t=d.getTime(); while(t+1000>d.getTime()) { d=new Date(); } } }
小型應用程式檢視器如下
#leave init
##now start
leave start
now paint: inits: 1starts: 1stops: 0
now paint: inits: 1starts: 1stops: 0 //縮放(放大)
now paint: inits: 1starts: 1stops: 0 //縮放(縮小)
#1)
now stop //重新啟動
leave stop
now init
leave init
now start
leave start
now paint: inits: 2starts: 2stops: 1
now stop
leave stop
將其嵌入到html裡
<HTML> <HEAD> <TITLE>WELCOME </TITLE> </HEAD> <BODY> test <APPLET code="cam1.class" WIDTH=750 HEIGHT=325> </APPLET> </BODY> </HTML>我用的是Google瀏覽器,網頁開啟如下
Java 插件10.13.2.20 使用 JRE 版本 1.7.0_13-b20 Java HotSpot(TM) Client VM 用户主目录 = C:\Users\Administrator ---------------------------------------------------- c: 清除控制台窗口 f: 终结在结束队列上的对象 g: 垃圾收集 h: 显示此帮助消息 l: 转储类加载器列表 m: 打印内存使用情况 o: 触发日志记录 q: 隐藏控制台 r: 重新加载策略配置 s: 转储系统和部署属性 t: 转储线程列表 v: 转储线程堆栈 x: 清除类加载器高速缓存 0-5: 设置跟踪级别为<n> ---------------------------------------------------- now init leave init now start now paint: inits: 1starts: 1stops: 0 leave start now paint: inits: 1starts: 1stops: 0 //缩放 now paint: inits: 1starts: 1stops: 0 now paint: inits: 1starts: 1stops: 0 now paint: inits: 1starts: 1stops: 0 now stop //刷新网页 leave stop Exception in thread "thread applet-cam1.class-1" java.lang.NullPointerException at java.awt.EventQueue.isDispatchThread(Unknown Source) at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDT(Unknown Source) at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.doClearAppletArea(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) now init leave init now start now paint: inits: 1starts: 1stops: 0 leave start###【相關推薦】######1. ##### #特別推薦######:###「php程式設計師工具箱」V0.1版本下載##########2. ###Java免費影片教學#######
#4. 教你如何去設定Applet的環境
以上是分享一個Java的小應用程式:applet的詳細內容。更多資訊請關注PHP中文網其他相關文章!