Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do you write HelloWorld in Applet?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to write Applet version of HelloWorld". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to write Applet version of HelloWorld" together!

Applet is a code written in Java that can run on the browser side. The obvious difference between Applet and application program lies in its different execution mode. Application program such as C program starts from main() program, while Applet is more complex. I'm not sure how complicated it is, but I'll get to know it better. An important property about Applets is that I can pass values in HTML as parameters to Applets (get parameter values by getParameter()). In this way, in order to produce different effects, we do not need to recompile Java programs, but only modify the HTML parameter values. Since HTML code can be generated dynamically, I can control the dynamic effects of the page as much as I want.

There are three main methods in the Applet lifecycle:init,start,stop

init(): responsible for initialization of Applet, executed only once throughout Applet life cycle. It's the same as the OnCreate() event in Delphi.

start(): The system automatically calls start() after calling init(), and calls this method every time the current window is reactivated, similar to the OnShow() event in Delphi.

stop(): Called after the user leaves the page where the applet is located. It allows you to stop resources from working when users are not paying attention to Applet so as not to affect system efficiency. And we don't need to artificially remove the method. This method is similar to the OnClose() event in Delphi.

Here is an Applet version of HelloWorld

Filename: HelloWorld.java

import java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

{

String title;

public void init(){

title="Hello World";

}

public void paint(Graphics g)

{

g.drawString(title,50,20);

}

}

We can see that there is no main function in the program, so how does it work. Since Applet runs in browser environment, we need to call it in HTML file. Need to use the relevant tags is the tag, we first create HTML file test.htm, the source code is as follows.

Here comes my first applet:

Put the file in the same directory as HelloWorld.java, compile HelloWorld.java, and click test.htm to open it. You can see Applet started. Alternatively, you can use AppletViewer test.htm to run the Applet without a browser.

The following program can better help us understand how Java Applet calls several of the methods described above throughout its lifecycle.

Filename: StartStop.java

import java.awt.*;

import java.applet.*;

public class StartStop extends Applet

{

StringBuffer message;

public void init()

{

message=new StringBuffer("Init done... ");

}

public void start()

{

message.append("Started... ");

}

public void stop()

{

message.append("Stopped... ");

}

public void paint(Graphics g)

{

g.drawString(message.toString(),150,50);

}

}

The operation method is the same as above. (This procedure can be referred to in the Machinery Industry)

Unlike C, it is much easier to implement GUI in Java. Because it is a purely object-oriented language, Java AWT provides various interface elements for us to call, just like Delphi components. Here is a comparison table of GUI objects in Java and corresponding components in Delphi

Java Delphi

Button TButton

Canvas TCanvas

Checkbox TCheckbox

CheckboxGroup TRadioGroup

Choice TComboBox

Label TLabel

TextField TEdit

TextArea TMemo

However,jdk is not a visual RAD(rapid application development) development tool, for the use of objects can not be as Delphi just drag, shift can, but we need to write call code. This creates a problem, how do I get elements to fit in the interface exactly as I want them to? When there are not many elements, you can let Java automatically layout (Layout), but when there are many elements, or when you need to arrange the elements according to the requirements of the application, you need to use Panel. Panel also has a corresponding component (TPanel) in Delphi, but it is mainly used to divide the interface and make rough layout. Accurate positioning also requires manual adjustment by developers. Java can only use Panel to locate, can not but say that it is a defect. Maybe I haven't learned yet.

After getting started, it's time to dive into the concept of objects.

Suppose you create a custom data type called Date in Java as follows

public class Date{

int day;

int month;

int year;

}

So what is different about java allocating memory for the following three statements that name variables?

(1) int i;

(2) Date mybirth;

(3) Date mybirth=new Date();

Obviously, there is, and the distribution is as follows:

(1)Java automatically allocates memory for integer i, usually two bytes

(2)Java declares an instance variable mybirth of the Date class and allocates storage space for it, but what is stored in this storage space is only a reference, or an address, and there is nothing in the current address, then we cannot use this instance variable, and cannot refer to its members.

(3)Java creates an instance variable mybirth of the Date class, allocates enough storage space for its member variables, and finally returns a reference to this storage space, that is, returns the first address of this storage space, and then accesses each member of this instance variable through mybirth, that is, this first address, such as mybirth.day,mybirth.month, mybirth.year.

When we declare a variable of primitive data type (boolean, byte, short, char, int,long,flat,double), the system automatically allocates memory for the variable. However, if String or user-defined variables are declared, the system does not immediately allocate memory for them. Why is that?

That's because String and user-defined variables belong to classes. A variable declared as a class is no longer data but a reference to data which means mybirth can be thought of as a pointer to an instance of the class which holds the address. That would make sense.

Deeper, since the instance variable value of a class is a pointer to an instance of a class, it is obvious that we can define instance variables of several classes with different names, all pointing to an instance. such as

University u=new University();//Defines an instance variable u of the class University and assigns it the object's storage space

University u2=u;//defines another instance variable u2, assigning the value of u to u2

So obviously u2 and u are the same thing except for their names, because they point to the same address.

I think it's important to know that. These data structure things are what a programmer needs to know.

Thank you for reading, the above is the "Applet version of HelloWorld how to write" content, after the study of this article, I believe we have a deeper understanding of how to write Applet version of HelloWorld this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report