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

相关文章

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析