`
snoopy7713
  • 浏览: 1140776 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多

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;

	@Override
	public void init() {
		// 在init方法中接收来自html页面上的参数
		//String message = getParameter("MESSAGE");
		add(new JLabel("welcome to touch's blog", JLabel.CENTER));
	}

	// 用main方法运行JApplet
	public 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;

	@Override
	public void init() {
		// 在init方法中接收来自html页面上的参数
		String message = getParameter("MESSAGE");
		add(new JLabel(message, JLabel.CENTER));
	}

	// 用main方法运行JApplet
	public 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>
  <applet
     code ="MyJApplet.class"
	 width=200
	 height=50
	 alt="you must have a java 2-enable browser to view the applet"
   >
   <param name=MESSAGE value="Welcome to touch's blog">
   </applet>
</html>

 运行结果:

 

分享到:
评论

相关推荐

    java常用类解析及示例及一些工具类源代码

    这是java常用类解析系列博客中的示例代码及自己写的工具类,代码注释详细,博客地址:http://blog.csdn.net/touch_2011/article/details/6860043 主要讲解了System类、Object类、Arrays类、Cloneable接口、IO系统...

    JavaApplet和JavaApplication

    ### JavaApplet与JavaApplication详解 #### 一、Java Application(应用程序) ##### 1.1 编写Java Application Java Application 类似于使用其他编程语言(如VB、VC)编写的桌面应用程序,通常以控制台方式运行...

    java+applet 聊天程序

    【Java+Applet聊天程序详解】 Java Applet是一种在客户端浏览器上运行的Java小程序,它扩展了网页的交互性,使得用户可以直接在浏览器中执行一些动态功能,如游戏、计算器或者像聊天应用这样的实时交互程序。本篇将...

    applet教程

    - 创建一个Applet类,继承自`java.applet.Applet` 或 `javax.swing.JApplet`。 - 实现必要的方法,如上述的`init()`, `start()`, `paint()` 等。 - 使用`&lt;applet&gt;` HTML标签将Applet嵌入到网页中,指定Applet类名...

    计算机等级考试二级Java练习题及解析

    Applet需要继承自`java.applet.Applet`或`javax.swing.JApplet`。与Application相比,Applet的执行环境是基于Web的,并可以通过HTML文件中的`&lt;applet&gt;`标签传递参数。 12. Applet安全限制:在Java 2中,Applet的...

    java实验9课程资源

    ### Java Applet 应用程序设计 #### 实验目的与要求 本次实验旨在帮助学习者深入了解Applet的工作原理,熟练掌握Java Applet程序的基本结构及其开发流程,同时探索Applet如何与浏览器进行通信。 #### 一、Applet...

    JAVA课后习题答案

    - **小应用程序(Applet):** 不需要定义`main`方法,而是继承自`java.applet.Applet`类或`javax.swing.JApplet`类。小应用程序主要运行在Web浏览器中。 #### 4. 说出Java源文件的命名规则 Java源文件的命名规则...

    JFreeChartApplet演示示例源码(eclipse部署可用)

    3. **创建 Applet**:接下来,创建一个继承自 `javax.swing.JApplet` 的类,并在其中添加 JFreeChart 到组件。这通常通过 `new ChartPanel(chart)` 实现,然后将其添加到 Applet 的容器中。 4. **Eclipse 部署**:...

    Java编程基础(2011-2012学年第一学期)复习提纲.doc

    Applet 必须继承自 `java.applet.Applet` 类或 `javax.swing.JApplet` 类,并且通常被嵌入到HTML页面中使用。 **3. 在DOS命令提示符下运行两种Java应用程序** - **Java 应用程序**: 先使用 `javac` 命令编译源代码...

    JAVA课程提纲

    3. **类源文件**:Java程序通常由多个类组成,这些类被保存在不同的文件中,每个文件通常对应一个类。类文件扩展名为`.java`,其中包含源代码。通过`javac`工具编译后生成的字节码文件扩展名为`.class`。 #### 知识...

    java计算器小程序

    ### Java计算器小程序知识点详解 #### 一、程序概述 该Java计算器小程序是一个纯Java实现的简单计算器应用,适用于Java编程初学者作为学习示例。它提供了基本的算术运算功能,如加法、减法、乘法和除法,并且支持...

    Java开发技术大全 电子版

    14.6.2小应用程序(JApplet)使用示例472 14.6.3对话框(JDialog)使用示例473 14.7中间容器476 14.7.1面板(JPanel)使用示例476 14.7.2滚动面板(JScrollPane)使用示例480 14.7.3分隔板(JSplitPane)使用...

    Java语言程序设计(一)04747汇总(2016——2017全)推荐文档.doc

    `int`:Java的基本数据类型之一,用于表示整型数值。 - B. `static`:用于修饰成员变量和成员方法,表示静态属性。 - C. `java`:这不是Java语言的关键字,而是Java编程语言本身的名字。 - D. `try`:用于异常...

    JAVA教学(关于swing的使用)

    4. **JApplet**:Swing版本的Applet类,用于构建可以在Web浏览器中运行的应用程序。 5. **JButton**:Swing中的按钮组件,可以设置文本、图标以及监听器来响应用户的点击事件。 6. **JCheckBox**:Swing中的复选框...

    javaSWT基础启蒙

    - `JApplet`:Swing的Applet容器,用于Web应用程序。 - `JDialog`:Swing的对话框容器。 - `JWindow`:Swing的无边框窗口。 这些容器继承自AWT中的`Container`类,但它们实现了更多的功能并提供了更好的用户体验。 ...

    详解如何使用Java编写图形化的窗口

    本篇文章将深入探讨如何使用Java来构建图形化的窗口,特别是基于Swing库的JFrame类。 首先,窗口在GUI编程中扮演着承载可视组件的角色。在Java中,有三种基本类型的窗口:Applet窗口、JFrame窗口和JWindow。Applet...

    jfreechart 用法

    `JApplet` 是Swing的一个基础类,通常用于构建图形用户界面的应用程序或applet。此外,还定义了一些变量,如 `cpuValue1`、`cpuValue2` 和 `cpuValue3`,用于存储CPU使用情况的数据。 ##### 3. 初始化时间序列数据 ...

Global site tag (gtag.js) - Google Analytics