Sie sind auf Seite 1von 9

Building Your First Java Applet

By Paul Leahy, About.com Guide

1 of 10 Previous Next

Introduction

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

Note: Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit. Java applets are like Java applications, their creation follows the same three step process of write, compile and run. The difference is, instead of running on your desktop, they run as part of a web page. The goal in this tutorial is to create a simple Java applet. This can be achieved by following these basic steps: 1. 2. 3. 4. Write a simple applet in Java Compile the Java source code Create a HTML page that references the applet Open the HTML page in a browser

Building Your First Java Applet


By Paul Leahy, About.com Guide

2 of 10 Previous Next

Write the Java Source Code

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

Im using Notepad to create my Java source code file. Open up your chosen editor, and type in this code: //Reference the required Java libraries import java.applet.Applet; import java.awt.*;

//The applet code public class FirstApplet extends Applet {

public void paint(Graphics g) {

//Draw a rectangle 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); } } Dont worry too much about what the code means. For your first applet, its more important to see how its created, compiled and run.

Building Your First Java Applet


By Paul Leahy, About.com Guide

3 of 10 Previous Next

Save the File

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

Save your program file as FirstApplet.java. Make sure the filename you use is correct. If you look at the code you will see the statement: public class FirstApplet extends Applet {

Its an instruction to call the applet class FirstApplet. The filename must match this class name, and have an extension of .java. If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.

Building Your First Java Applet


By Paul Leahy, About.com Guide

4 of 10 Previous Next

Open a Terminal Window

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

To open a terminal window, press the Windows key and the letter R. You will now see the Run Dialog. Type cmd, and press OK. A terminal window will appear. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files that they contain, and run any programs that you want to. This is all done by typing commands into the window.

Building Your First Java Applet


By Paul Leahy, About.com Guide

5 of 10 Previous Next

The Java Compiler

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

We need the terminal window to access the Java compiler called javac. This is the program that will read the code in the FirstApplet.java file, and translate it into a language that your computer can understand. This process is called compiling. Just like Java applications, Java applets must be compiled too. To run javac from the terminal window, you need to tell your computer where it is. On my machine, its in a directory called C:\Program Files\Java\jdk1.6.0_06\bin. If you dont have this directory, then do a file search in Windows Explorer for javac and find out where it lives. Once youve found its location, type the following command into the 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 wont do anything flashy, it will just return to the command prompt. However, the path to the compiler has now been set.

Building Your First Java Applet


By Paul Leahy, About.com Guide

6 of 10 Previous Next

Change the Directory

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

Navigate to where the FirstApplet.java file is saved. Ive saved my file in the location C:\Documents and Settings\Paul\My Documents\Java\Applets. To change the directory in the terminal window, type in the command: cd *directory where FirstApplet.java file is saved* E.g.,

cd C:\Documents and Settings\Paul\My Documents\Java\Applets You can tell if youre at the right directory by looking to the left of the cursor.

Building Your First Java Applet


By Paul Leahy, About.com Guide

7 of 10 Previous Next

Compile the Applet

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

Were now ready to compile the applet. To do so, enter the command: javac FirstApplet.java After you hit Enter, the compiler will look at the code contained within the FirstApplet.java file, and attempt to compile it. If it cant, it will display a series of errors to help you fix the code. The applet has been compiled successfully if you are returned to the command prompt without any messages. If thats not the case, go back and check the code youve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors. Tip: Once the applet has been successfully compiled, you will see a new file in the same directory. It will be called FirstApplet.class. This is the compiled version of your applet.

Building Your First Java Applet


By Paul Leahy, About.com Guide

8 of 10

Create the HTML File

Previous Next

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

Its worth noting that so far you have followed exactly the same steps as you would if you were creating a Java application. The applet has been created and saved in a text file, and it has been compiled by the javac compiler. Java Applets differ from Java applications when it comes to running them. Whats needed now is a web page that references the FirstApplet.class file. Remember, the class file is the compiled version of your applet; this is the file your computer can understand and execute. Open up Notepad, and type in the following HTML code: <HTML> <HEAD> <TITLE>My First Java Applet</TITLE> </HEAD> <BODY> Here's my first Java Applet: <BR><BR>

<applet code="FirstApplet.class" width="300" height ="300">

</BODY> </HTML> Save the file as MyWebpage.html in the same directory as your Java applet files.

This is the most important line in the webpage: <applet code="FirstApplet.class" width="300" height ="300"> When the web page is displayed, it tells the browser to open up your Java applet and run it. Previous

Building Your First Java Applet


By Paul Leahy, About.com Guide

9 of 10 Previous Next

Open the HTML Page

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

The last step is the best one; we get to see the Java applet in action. Use Windows Explorer to navigate to the directory where the HTML page is stored. For example, my page is stored in C:\Documents and Settings\Paul\My Documents\Java\Applets with my other Java applet files. Double-click on the MyWebpage.html file. Your default browser will open, and the Java applet will run. Congratulations, you have created your first Java applet!

Building Your First Java Applet


By Paul Leahy, About.com Guide

10 of 10 Previous Next

A Quick Recap

Take a moment to review the steps you took to create the Java applet. They will be the same for every applet you make: 1. 2. 3. 4. 5. 6. Write the Java code in a text file Save the file Compile the code Fix any errors Reference the applet in a HTML page Run the applet by viewing the web page Previous Next Install the Java SE Development Kit

See More About: getting started with java applets

Must Reads What Is Java? Download the Java Development Kit Install the Java SE Development Kit Create Your First Java Program Reserved Words in Java

Paul Leahy Java Guide

Das könnte Ihnen auch gefallen