Java Applets are small applications written in Java language. They can be directly embedded into web pages and can produce special effects.
After the applet is compiled, it will Generate .class files and embed the .class files in html web pages. As long as the user connects to a web page, the applet will be downloaded to the user's computer along with the web page and run
The inheritance relationship of the applet class is as follows:
java.lang.Object
## java.awt.Component
java.awt.Container
java.awt.Panel
## java.applet.AppletThe life cycle of applet
##The four main methods of applet
public void init(): Called by the browser or appletviewer to tell the current applet that it has been loaded into the system. This method is always called before the start() method is called for the first time.
public void start(): Called by the browser or appletviewer to tell the current applet that it should start executing. This method is called after the init() method, and this method is called every time the web page accesses the applet
public void stop(): Called by the browser or appletviewer to tell the current applet that it should stop execution. This method is called when the Web page containing the current applet is replaced by another Web page. This method is also called before calling the destroy() method. Method
public void destroy(): Called by the browser or appletviewer to tell the current applet that it has been asked to return and that it should clear any resources assigned to it
Please see the following code
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(); } } }
##eclipse The Console in is as follows
leave init
now start
leave start
now paint: inits: 1starts: 1stops: 0
now paint: inits: 1starts: 1stops: 0 //Zoom (zoom in)
now paint: inits: 1starts: 1stops: 0 //Zoom (zoom out)
now stop //Restart
leave stop
now init
##leave initnow start
leave start
now paint: inits: 2starts: 2stops: 1
now stop
leave stop
Embed it in html
<HTML>
<HEAD>
<TITLE>WELCOME </TITLE>
</HEAD>
<BODY> test
<APPLET code="cam1.class" WIDTH=750 HEIGHT=325>
</APPLET>
</BODY>
</HTML>
## View the output of #System.out.println in the java console
Before setting the java console to display in the java control panel
When the window is resized, moved, or its contents change, paintFunction
The console displays as follows
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
Special Recommendation
"php Programmer Toolbox" V0.1 version download
2. Java Free Video Tutorial
3. Take you to get to know the Java Applet program
4. Teach you how to configure the Applet environment
5. Detailed explanation of the differences between Application and Applet
The above is the detailed content of Share a small Java application: applet. For more information, please follow other related articles on the PHP Chinese website!