首頁  >  文章  >  Java  >  Java 小程式

Java 小程式

王林
王林原創
2024-08-30 16:05:24964瀏覽

在 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