首页  >  文章  >  Java  >  Java 小程序

Java 小程序

王林
王林原创
2024-08-30 16:05:24966浏览

在 Java 中,开发人员可以使用 applet 概念将 Web 浏览器和页面无缝结合,从而产生特定的输出。 Java 中的 Applet 的范围可以从简单的 Applet 到非常复杂的 Applet。一个简单的 Hello World 小程序可以让人们了解基本的 Java 小程序如何工作以及如何在屏幕上显示。在本文中,我们将探讨基本的 Java applet 和另一个在解决各种实际问题中广泛使用的 Java applet。 Java 小程序可以实现为多种其他编程语言,例如 Python、Dot Net 和其他代码。

Java 小程序的生命周期

在这个阶段,我们将观察小程序的生命周期。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

小程序的生命周期有五个基本阶段。

Java 小程序

  1. 初始化
  2. 开始
  3. 画的
  4. 停止了
  5. 被摧毁

让我们看看这 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 的表示。

Java 小程序

每当我们尝试运行小程序时,小程序都会提供下面的屏幕,其中向我们显示了许多选项,可供我们从一系列选项中进行选择。在下面的屏幕中,我们可以生成网页、在 Applet Viewer 中运行 applet 或在 Web 浏览器中运行 applet。我们提供了多种在 BlueJ 编程平台中运行小程序的选项。盒子里还可以提供小程序的高度和宽度,根据提供的高度和宽度,小程序运行,形成一个独特的小程序,为各种问题提供解决方案。

Java 小程序

现在我们看到示例编码和导入包的示例来呈现 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 小程序

该示例还提供了示例输出。开发人员可以在 Java 小程序中包含附加注释以生成各种其他输出。

输出:

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:

Java 小程序

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn