Applet Example
|
This routine prints out Hello World to a browser
window:
import
java.applet.Applet;
import
java.awt.Graphics;
public
class HelloWorld extends Applet {
public
void paint(Graphics g) {
g.drawString("Helloworld!"
,50,25);
}
}
Put this code into a file called HelloWorld.java
Then compile the application with
> javac HelloWorld.java
This creates the class file
HelloWorld.class
Create an html file to hold the applet. Put the following code into a file called HelloWorld.html in the
SAME directory as the class file.<HTML>
<APPLET CODE="HelloWorld.class"
WIDTH=150 HEIGHT=50>
</APPLET>
</BODY>
</HTML>
You need to upload the class and html on the webserver to make it work.
Creating a Simple Java Applet WITH JBUILDER
The following exercise will help you become familiar with Borlands JBuilder 3 Development Environment. This is an at-home exercise for your personal computer and is recommended following the installation of JBuilder3.
Start Borland's JBuilder3
Click the "Start" button
Select: Programs | Borland JBuilder3 University | JBuilder3
From the Menu Bar,
Select: File | Close to close any open projects or files
From the Menu Bar
Select: File | Open
When the Open dialog box appears:
If you want this project to be saved to floppy:
In the "Look in:" Drop-Down box, Left Click the Down Arrow
Left Click "3½ Floppy (A:)"Left Click on the "Create New Folder" Button
Type Exercise2 as the folder name
Double-click on the "Exercise2" folder
Left Click in the "File name:" boxIn the "Filename:" box, remove any existing text and type:
Exercise2.jpr
Click the "Open" buttonA message dialog box will appear stating the file does not exist.
Click the "Yes" button to the question, "Create a new file?"
From the Menu Bar
Select: Project | Properties
Modify the Root Directories as follows:Source:
C:\Java\Exercise2
-or-
A:\Exercise2Output:
C:\Java\Exercise2
-or-
A:\Exercise2
Click the "OK" button
From the Menu Bar
Select: Project | Add to Project
Enter Filename: "WelcomeLines.java"
Check "Add to Project" box
Click the "Open" button
Type in lines 1 - 13 exactly as shown in Fig 3.10 on page 93 of the text
(do not type the line numbers)From the Menu Bar
Select: File | Save
From the Menu Bar
Select: Project | Add to Project
Enter Filename: "WelcomeLines.html"
Check "Add to Project" box
Click the "Open" buttonType in lines 1 - 4 exactly as shown in Fig 3.11 on page 93 of the text
(do not type the line numbers)From the Menu Bar
Select: File | Save
From the Menu Bar,
Select: Run | Run "WelcomeLines.java"
If the program does NOT work and there are errors in the message window, correct the errors and try to "Run" the program again. Repeat until the program works.