JAVA-based Applet

Source: Internet
Author: User

Applet (small application) uses HTML-based programs created in Java. The browser temporarily downloads the file to the user's hard disk and runs it locally when the web page is opened. Generally, an Applet can only be run through appletviewer or a browser. Generally, Java programs can be embedded into webpages to run by inheriting the Applet Class.

Java is much easier to get started with than I thought. So far I have understood the basic syntax structure of Java. However, I know that it takes time and practice to thoroughly study any language.

The Applet is a Java-compiled code that can be run on the browser. The obvious difference between Applet and application is that its execution method is different. For example, the C program runs from main () the main program starts to run, while the Applet is more complex. I don't know how complicated it is, but I will know it slowly. An important property of Applet is that I can pass the value in HTML to Applet as a parameter (get the parameter value through getParameter ). in this way, in order to produce different results, we do not need to re-compile the Java program, but only need to modify the HTML parameter value. Because HTML code can also be dynamically generated, I can control the dynamic effect of webpages as I like.

There are three main methods in the Applet life cycle: init, start, stop.

Init (): initializes the Applet. This method is executed only once throughout the Applet lifecycle. It is the same as the OnCreate () event in Delphi.

Start (): After the system calls init (), it will automatically call start (), and this method will be called every time the current window is re-activated, and OnShow () in Delphi () events are similar.

Stop (): This method is called after the user leaves the page where the Applet is located. It allows you to stop some resources when you do not pay attention to the Applet, so as not to affect the system operation efficiency. we do not need to manually remove this method. this method is similar to the OnClose () event in Delphi.

The following is an Applet version of HelloWorld.

File Name: 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 run. since the Applet runs in the browser environment, we need to call it in the HTML file. the corresponding tag is the <Applet> tag. We first create the HTML file test.htm. The source code is as follows:

 

<Html>

<Body>

Here comes my first applet:

<Br>

<Applet code = HelloWorld. class width = 650 height = 500>

</Applet>

</APPLET>

</Body>

</Html>

 

Put the file and helloworld.javain the same directory. After compiling helloworld.java, click test.htm to open the file. You can see that the Applet is started. Or you can run the Applet directly using the AppletViewer command AppletViewer test.htm without the browser.

The following program helps us better understand how Java Applet calls the methods described above throughout the lifecycle.

File Name: 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 running method is the same as above (this program can be referred to the <Java concise tutorial> of the machinery industry)

Different from the C language, it is much easier to use Java to implement GUI. Java AWT provides various interface elements for us to call because it is a pure object-oriented language, just like the components in Delphi. The following table compares the GUI objects in Java with the corresponding components in Delphi.

 

However, JDK is not a visualized RAD (rapid application development) development tool. For objects, you cannot just drag or shift them like Delphi. Instead, you need to write the call code. this creates a problem. How can I place elements on the interface as per my requirements? When there are not many elements that allow Java to automatically Layout (Layout), but when there are many elements, or you need to place elements according to application requirements, you need to use a Panel ). the Panel also has corresponding components (TPanel) in Delphi, but it is mainly used to split the interface and make a rough layout. Precise positioning also requires manual adjustment by developers. java can only be identified by Panel, which is a defect. maybe I haven't learned it yet.

After getting started, you should go deep into the object concept.

Assume that a custom data type named Date is created in Java as follows:

Public class Date {

Int day;

Int month;

Int year;

}

 

So what are the differences between java's memory allocation for the following three statements with famous variables?

Int I;

Date mybirth;

Date mybirth = new Date ();

 

Obviously, the allocation is as follows:

(1) Java automatically allocates memory for integer I for integer variables, generally two bytes.

(2) Java declares the instance variable mybirth of the Date class and allocates a bucket for it. However, the bucket stores only a reference or an address, there is nothing in the current address, so we cannot use this instance variable or reference its members.

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

When we declare a variable of the basic data type (such as boolean, byte, short, char, int, long, flat, and double), the system automatically allocates memory for the variable. However, if a String or user-defined variable is declared, the system will not immediately allocate memory to it. Why?

This is because both String and User-Defined variables belong to the category of the class. A variable declared as a class is no longer a data, but a reference to the data, that is, mybirth can be seen as a pointer to an instance of the class, it stores the address. in this case, you can understand it.

Further down, since the instance variable value of the class is a pointer and this pointer points to an instance of the class, we can clearly define multiple instance variables of different classes, and point them all to an instance. For example:

 

University u = new University (); // defines the instance variable u of A University class and allocates the object storage space for it.

University u2 = u; // another instance variable u2 is defined to assign the u value to u2.

 

Obviously, u2 and u have different names, because they point to the same address.

I think it is important to clarify this point. These data structures are what a programmer needs to know.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.