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全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo