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

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr