Week Five: Applets introduction

Source: Internet
Author: User
Tags file url

1. Definition  

Applets are small applications written in the Java programming language that can be included in the HTML ( Standard Universal Markup Language one of the applications)page, the same way you would include an image in a page.[1]The HTML file code of the Web page containing the applet has a pair of tags such as <applet> and </applet>, when a Java-enabled web browser encounters this pair of tags, The appropriate applet code is downloaded and executed on the local computer.

2. Development steps and examples

1) Use tools such as edit or Windows Notepad as the editor to create Java applet source programs. 2) Convert the applet's source program to a bytecode file. 3) Compile the HTML file using class. Put the necessary <OBJECT> statements inside the HTML file. Here is a simple HelloWorld example to illustrate the applet development process: (1) Editing the Java source file for the appletCreate the folder C:\GHQ, under which the source code for the Helloworld.java file is created as follows:
import java.awt.*; import java.applet.*;  Public class extends // Inherit the applet class, which is the feature of the applet Java program {publicvoid  paint (Graphics g) {g.drawstring ("Hello world!", 5,35);}}

Save the above program in the C:\ghq\HelloWorld.java file.

(2) Compiling appletsCompile the Helloworld.java source file using the following JDK command: C:\ghq\>javac helloworld.java<enter> Note: If you write a source program that violates the syntax rules of the Java programming language, The Java compiler will display syntax error messages on the screen. The source file must contain no syntax errors, and the Java compiler can successfully convert the source program to a bytecode program that appletviewer and the browser can execute. After the Java applet is successfully compiled, the Helloworld.class file is generated in response to the bytecode file. Listing the directory list with the Explorer or dir command will find a file named Helloworld.class in the directory C:\GHQ. (3) Creating an HTML fileBefore you run the created Helloworld.class, you also need to create an HTML file that appletviewer or the browser will use to access the created applet. To run Helloworld.class, you need to create a file named Helloworld.html that contains the following HTML statements.
<HTML><TITLE>helloworld! Applet</TITLE><appletsCODE= "Helloworld.class"//This should be helloworld.classwidth=200HEIGHT=100></applets></HTML>

In this example, the,<applet> statement indicates the APPLET's byte-code class file name and the size of the window in pixels. Although this HTML file uses the file name helloworld.html, it corresponds to the name of Helloworld.java, but this correspondence is not necessary, you can use any other name (such as ghq.html) to name the HTML file. However, keeping the file name in a corresponding relationship can be convenient for managing the files.

(4) Execution helloworld.htmlIf you run helloworld.html with appletviewer, you need to enter the following command line: C:\ghq\>appletviewer helloworld.html<enter> can see that The command launches Appletviewer and indicates the HTML file that contains the <APPLET> statement that corresponds to HelloWorld. If you run the HelloWorld Applet in a browser, enter the HTML file URL address in the browser's address bar. At this point, the entire process of developing a applets program is complete (including Java source files, compiled class files, HTML files, and running with Appletviewer or browser).
Importjava.awt.*;Importjava.applet.*; Public classTestextendsApplet//inherit the Appelet class, which is the feature of the applet Java program{ Public voidPaint (Graphics g) {people Zhang;zhang=Newpeople (); Zhang.setname ("Robot"); Zhang.setage (12); g.DrawString (Zhang.talk (),5,35);}}classpeople{PrivateString name;Private intAge ; String talk () {return"I Am" +name+ ", this year" +age+ "year";} Public voidsetname (String str) {name=str;} Public voidSetage (inta) {if(a>0) Age=A;Elseage=-A;} PublicString getname () {returnname;} Public intGetage () {returnAge ;}}

Week Five: Applets introduction

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.