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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。