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(); } } }The applet viewer is as follows
##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>
I am using Google Chrome and the web page opens as follows
## 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[Related recommendations]1.
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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
