java常用类解析九:Applet(JApplet)详解及示例

2024-01-15 09:58

本文主要是介绍java常用类解析九:Applet(JApplet)详解及示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、Applet类及各个方法说明

     Applet类提供一个基本框架,使得applet可以通过Web浏览器来运行,applet没有main方法,它依靠浏览器调用Applet类中的方法。Applet不安全。下面是截取的一段Applet类的源代码:

/*** Called by the browser or applet viewer to inform this applet that it has* been loaded into the system. It is always called before the first time* that the <code>start</code> method is called.* <p>* A subclass of <code>Applet</code> should override this method if it has* initialization to perform. For example, an applet with threads would use* the <code>init</code> method to create the threads and the* <code>destroy</code> method to kill them.* <p>* The implementation of this method provided by the <code>Applet</code>* class does nothing.* 补充:JVM加载applet类,浏览器创建applet,浏览器调用init方法进行初始化,如果Applet的子* 类具有初始化操作应覆盖此方法。通常,该方法实现的功能包括创建用户界面组件、装载图像和音频等资源* 以及从HTML网页的<applet>标记中获取参数*/public void init() {}/*** Called by the browser or applet viewer to inform this applet that it* should start its execution. It is called after the <code>init</code>* method and each time the applet is revisited in a Web page.* <p>* A subclass of <code>Applet</code> should override this method if it has* any operation that it wants to perform each time the Web page containing* it is visited. For example, an applet with animation might want to use* the <code>start</code> method to resume animation, and the* <code>stop</code> method to suspend the animation.* <p>* Note: some methods, such as <code>getLocationOnScreen</code>, can only* provide meaningful results if the applet is showing. Because* <code>isShowing</code> returns <code>false</code> when the applet's* <code>start</code> is first called, methods requiring* <code>isShowing</code> to return <code>true</code> should be called from* a <code>ComponentListener</code>.* <p>* The implementation of this method provided by the <code>Applet</code>* class does nothing.* 补充:init方法完成后,调用start方法,浏览过别的网页之后回来也调用此方法*/public void start() {}/*** Called by the browser or applet viewer to inform this applet that it* should stop its execution. It is called when the Web page that contains* this applet has been replaced by another page, and also just before the* applet is to be destroyed.* <p>* A subclass of <code>Applet</code> should override this method if it has* any operation that it wants to perform each time the Web page containing* it is no longer visible. For example, an applet with animation might want* to use the <code>start</code> method to resume animation, and the* <code>stop</code> method to suspend the animation.* <p>* The implementation of this method provided by the <code>Applet</code>* class does nothing.* 补充:离开页面时调用stop方法*/public void stop() {}/*** Called by the browser or applet viewer to inform this applet that it is* being reclaimed and that it should destroy any resources that it has* allocated. The <code>stop</code> method will always be called before* <code>destroy</code>.* <p>* A subclass of <code>Applet</code> should override this method if it has* any operation that it wants to perform before it is destroyed. For* example, an applet with threads would use the <code>init</code> method to* create the threads and the <code>destroy</code> method to kill them.* <p>* The implementation of this method provided by the <code>Applet</code>* class does nothing.*/public void destroy() {}
 

从以上描述和代码中可以看出,浏览器通过init、start、stop、destroy方法控制Applet,通常这些方法都是空方法,一般要覆盖这些方法实现操作。

2、JApplet类示例

     Applet类没有考虑与Swing组件一起工作,所以从Applet类扩展出了一个JApplet类。JApplet的内容窗格使用BorderLayout布局管理器。下面是一个JApplet的实际例子:

package demo.others;import java.awt.BorderLayout;import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MyJApplet extends JApplet {private static final long serialVersionUID = 1L;@Overridepublic void init() {// 在init方法中接收来自html页面上的参数//String message = getParameter("MESSAGE");add(new JLabel("welcome to touch's blog", JLabel.CENTER));}// 用main方法运行JAppletpublic static void main(String[] args) {JFrame frame = new JFrame("Applet is in the frame");MyJApplet myJApplet = new MyJApplet();// main方法里创建一个框架来放置applet,applet单独运行时,// 要完成操作必须手动调用init和start方法frame.add(myJApplet, BorderLayout.CENTER);myJApplet.init();frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 300);frame.setVisible(true);}
}

 下面的例子用html显示Applet

import java.awt.BorderLayout;import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MyJApplet extends JApplet {private static final long serialVersionUID = 1L;@Overridepublic void init() {// 在init方法中接收来自html页面上的参数String message = getParameter("MESSAGE");add(new JLabel(message, JLabel.CENTER));}// 用main方法运行JAppletpublic static void main(String[] args) {JFrame frame = new JFrame("Applet is in the frame");MyJApplet myJApplet = new MyJApplet();// main方法里创建一个框架来放置applet,applet单独运行时,// 要完成操作必须手动调用init和start方法frame.add(myJApplet, BorderLayout.CENTER);myJApplet.init();frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 300);frame.setVisible(true);}
}
 
<html><head><title>passing string to java Applets</title></head><body><p>this applet gets a message from the HTML</p><appletcode ="MyJApplet.class"width=200height=50alt="you must have a java 2-enable browser to view the applet"><param name=MESSAGE value="Welcome to touch's blog"></applet>
</html>

 运行结果:

 

这篇关于java常用类解析九:Applet(JApplet)详解及示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/608506

相关文章

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映