Teach you to build the first Java applet program _java

Source: Internet
Author: User
Tags save file java se

Introduced

Note: Before you begin this tutorial, you must download downloaded and install installed Java SE Development Kit.

Java applets, like Java applications, are built to follow the same three steps-write, compile, and run. The difference is that they run on a part of the Web page, not on your desktop.

The main purpose of this article is to create a simple Java applet. To achieve this, follow these three basic steps:

1. Write a simple applet in Java

2. Compiling Java source code

3. Create an HTML page that involves an applet

4. Open HTML page in browser

Writing Java source code

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

I use Notepad to create my Java source code files. Open the editor you selected and enter the code like this:

Reference the required Java libraries
import Java.applet.Applet;
Import java.awt.*;
The Applet code public
class Firstapplet extends applets {public
void paint (Graphics g) {
//draw a rec  Tangle width=250, height=100
g.drawrect (0,0,250,100);
Set the color to Blue
g.setcolor (color.blue);
Write the message to the Web page
g.drawstring ("Look at me, I ' m a Java applet!", 10,50);
}
}

Don't worry too much about the meaning of the code. This is your first applet and it is important to look at how it was created, compiled, and run.

Save File

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

Save your program file as "Firstapplet.java". Make sure that the file name you are using is correct. If you see the code shown below:

public class Firstapplet extends Applet {

This is an instruction to invoke the applet class "Firstapplet". The filename should match the name of the class and have a ". Java" extension. If your file is not saved as "Firstapplet.java", the Java compiler will complain and will not compile your applet.

Open a terminal window

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

To open a terminal window, press "Windows key" and the letter "R".

You will see "Run Dialog". Enter "cmd" and click "OK".

The terminal window is displayed. It looks like a text version of Windows Explorer; it lets you hit a different directory on your computer, look at the files they contain, and run the program you want. You can do this by typing the command in the window.

Java Compiler

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

We need a terminal window to access the Java compiler called "Javac". This is a program to read the code in the Firstapplet.java file and translate it into a language that your computer can know. This process is compiled. Like Java applications, Java applets must be compiled as well.

In order to run Javac in the terminal window, you need to tell the computer where it is. On my machine, it is in the directory "C:\Program Files\java\jdk1.6.0_06\bin". If you do not have such a directory, search for the file "Javac" in Windows Explorer and find its location.

Once you find out where it is, enter the following command path terminal window:

Set path= *the directory where Javac lives*

e.g.,

Set Path=c:\program Files\java\jdk1.6.0_06\bin

Press ENTER. The terminal window doesn't do anything flashy, it just returns to the command prompt. However, the compiler path has now been set.

Change Directory

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

Take a look at where to save the Firstapplet.java file. The location of my files is "C:\Documents and Settings\paul\my documents\java\applets".

To change the directory in the Terminal window, enter the following command:

CD *directory where Firstapplet.java file is saved*

e.g.,

CD C:\Documents and Settings\paul\my documents\java\applets

By looking at the cursor on the left you can tell you are in the right directory. We are now ready to compile the applet. Enter command:

Javac Firstapplet.java

After clicking Enter, compiler will see the code contained in the Firstapplet.java file and try to compile it. If it doesn't, it will show a series of mismatches to help you fix the code.

If you return to the command prompt without any information, your applet compiles successfully. If that's not the case, go back and check the code you wrote. Make sure it matches the example code and save the file again. Keep doing this until you run the Javac without any error prompts.

Tip: Once applets compiles successfully, you will see a new file in the same directory. It's called "Firstapplet.class." This is the version of your applet that has been compiled.

Create an HTML file

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

It is worth noting that so far you have exactly followed the same steps if you are creating a Java application. The applet is created and saved in a text file that has been compiled through Javac compiler.

Java applets are different from Java applications when they are running. What you need now is a Web page that involves Firstapplet.class files. Remember, the class file is a compiled version of your applet; This is a file that your computer can know and execute.

Open Notepad and enter the following HTML code:

<HTML>
<HEAD>
<title>my-Java applet
</HEAD>
<BODY> here
' s My The Java Applet: 
<applet code= "Firstapplet.class" width= "+" height = "> </BODY>"
</HTML>

In the same directory, save the file as "mywebpage.html" as your Java applet file.

This is the most important line in the Web page:

< applet code= "Firstapplet.class" width= "+" height = ">"

When the page is displayed, it tells the browser to open your Java applet and run it.

Open HTML page

Microsoft product screen shot (s) reprinted with permission from Microsoft Corporation.

The final step is also the best one; we can see that Java applets are starting to run. Use Windows Explorer to navigate through the directories stored in an HTML page. For example, my Web page is saved in "C:\Documents and Settings\paul\my documents\java\applets" and my other Java applet file.

Double-click the mywebpage.html file. Your default browser will open and the Java applet will run.

Congratulations, you've created your first Java applet!.

Quick Little Summary

Take a moment to summarize the steps to create a Java applet. They are the same for every applet you create:

1. Writing Java code in a text file

2. Save the file

3. Compiling code

4. Fixing errors

5. Reference applet in HTML Also

6. Run the applet by browsing the web

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.