在 Java 中,开发人员可以使用 applet 概念将 Web 浏览器和页面无缝结合,从而产生特定的输出。 Java 中的 Applet 的范围可以从简单的 Applet 到非常复杂的 Applet。一个简单的 Hello World 小程序可以让人们了解基本的 Java 小程序如何工作以及如何在屏幕上显示。在本文中,我们将探讨基本的 Java applet 和另一个在解决各种实际问题中广泛使用的 Java applet。 Java 小程序可以实现为多种其他编程语言,例如 Python、Dot Net 和其他代码。
Java 小程序的生命周期
在这个阶段,我们将观察小程序的生命周期。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
小程序的生命周期有五个基本阶段。
- 初始化
- 开始
- 画的
- 停止了
- 被摧毁
让我们看看这 5 个阶段的作用:
- 小程序通过利用 init() 函数实现初始化,包括设置执行所需的所有变量。
- 小程序已启动。完成初始化步骤后,小程序使用 start() 函数开始执行并继续。
- 当小程序执行绘画过程时,paint() 方法负责创建和执行小程序的基本功能。
- 小程序已停止。
- 小程序是destroy()函数负责从主存中移除所有内容,指示移除过程。
使用 Java 开发 Applet
每当小程序运行时,它都会顺序调用多个函数来确定主要目标并生成多个输出。
小程序依次调用以下函数:
- Init()= init()是在程序开始时调用的函数,用于为程序分配不同的变量并实时执行。
- Start()– Start() 通常在 init() 之后调用,调用它是为了查看随 Java 编码一起出现的 HTML 屏幕。每当用户离开打开的 HTML 屏幕并返回时,他都会从 start() 开始,跳过 init ()。
- Paint()– 小程序通过调用 Paint() 方法开始执行程序。 Paint() 有一个称为 Graphics 的参数,Graphics g 有助于程序执行绘制不同的参数。
- Stop() – 当用户离开 HTML 文档并移动到小程序时调用 Stop()。每当 Stop() 终止程序时,Start() 都会重新启动它们,然后应用更改。
- Destroy()– 当需要从内存中完全删除时,小程序会调用 Destroy() 函数。
Java 小程序示例
下面,我们提供了几个 Java 中的 Applet 示例:
示例#1
在编码示例中,我们将看到 Hello World 小程序的基本版本。下图说明了 BlueJ Java 编程平台中一个基本 Applet Hello World 的表示。
每当我们尝试运行小程序时,小程序都会提供下面的屏幕,其中向我们显示了许多选项,可供我们从一系列选项中进行选择。在下面的屏幕中,我们可以生成网页、在 Applet Viewer 中运行 applet 或在 Web 浏览器中运行 applet。我们提供了多种在 BlueJ 编程平台中运行小程序的选项。盒子里还可以提供小程序的高度和宽度,根据提供的高度和宽度,小程序运行,形成一个独特的小程序,为各种问题提供解决方案。
现在我们看到示例编码和导入包的示例来呈现 Hello World 示例代码。在这个程序中,我们实现了Applet和Graphics等包。开发人员使用 Graphics g 对象在网络浏览器或任何所需的介质中绘制和渲染输出。
代码:
import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { // Overriding paint() method @Override public void paint(Graphics g) { g.drawString("Hello World@ Great to be here!!!", 50, 50); } }
输出:
该示例还提供了示例输出。开发人员可以在 Java 小程序中包含附加注释以生成各种其他输出。
输出:
Now, we can do many functions on the applet, such as Restart, Reload, Stop, Save, Start, and Clone. These are functionalities that the applet provides with various stages.
Example #2
In the next coding example, we will see more aspects of Java applets in which we see the basic functionality of Java applets. In the next Java applet, we change the height and width of the Java applet, and we make some fundamental changes in the functionality. The writing comes more in the middle. Below is an example of a coding sample that executes to print the “Hello World” statement:
Code:
import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { // Overriding paint() method @Override public void paint(Graphics g) { g.drawString("Hello World!!! Great to be here!!!", 150,150); } }
Output:
Advantages and Disadvantages of Applets in Java
There are several advantages and disadvantages of running a Java applet.
Advantages
Some of the advantages listed down are as follows:
- The applet works on the client side; hence there is a meager response time.
- The applet is very secure and robust.
- It can execute under various operating systems, including Unix, Linux, and macOS operating systems.
- Combining the applet with HTML, CSS, and Javascript enhances the web page’s functionality.
- The applet is very easy to build and contains very little redundancy.
Disadvantages
The document can illustrate the non-use support and highlight some of the disadvantages of Applets. The disadvantages are:
- The client side requires the presence of a plugin to execute the applet.
- Without the proper plugin, there is no execution of the applet on the client side.
- Making complex applets is quite complicated and hence is a disadvantage.
Conclusion
In this article, we see the basic functions of an applet, the lifecycle of an applet, as well as some basic programs as to how an applet runs in the Java programming language. Developers can create and implement highly complex applets using software like BlueJ and other tools for HTML and CSS. We stress the theory part of applets more than the programming concept for applets.
以上是Java 小程序的详细内容。更多信息请关注PHP中文网其他相关文章!

Java在企业级应用中被广泛使用是因为其平台独立性。1)平台独立性通过Java虚拟机(JVM)实现,使代码可在任何支持Java的平台上运行。2)它简化了跨平台部署和开发流程,提供了更大的灵活性和扩展性。3)然而,需注意性能差异和第三方库兼容性,并采用最佳实践如使用纯Java代码和跨平台测试。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)橱柜橱柜:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

Java适合开发跨服务器web应用。1)Java的“一次编写,到处运行”哲学使其代码可在任何支持JVM的平台上运行。2)Java拥有丰富的生态系统,包括Spring和Hibernate等工具,简化开发过程。3)Java在性能和安全性方面表现出色,提供高效的内存管理和强大的安全保障。

JVM通过字节码解释、平台无关的API和动态类加载实现Java的WORA特性:1.字节码被解释为机器码,确保跨平台运行;2.标准API抽象操作系统差异;3.类在运行时动态加载,保证一致性。

Java的最新版本通过JVM优化、标准库改进和第三方库支持有效解决平台特定问题。1)JVM优化,如Java11的ZGC提升了垃圾回收性能。2)标准库改进,如Java9的模块系统减少平台相关问题。3)第三方库提供平台优化版本,如OpenCV。

JVM的字节码验证过程包括四个关键步骤:1)检查类文件格式是否符合规范,2)验证字节码指令的有效性和正确性,3)进行数据流分析确保类型安全,4)平衡验证的彻底性与性能。通过这些步骤,JVM确保只有安全、正确的字节码被执行,从而保护程序的完整性和安全性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器