Professional Documents
Culture Documents
Jbuilder Lesson 1 - Hello World!: Figure 1.0 - Project Wizard Prompts You For Details About Your Project
Jbuilder Lesson 1 - Hello World!: Figure 1.0 - Project Wizard Prompts You For Details About Your Project
Overview
The goal of this lesson is help you to become familiar with the Borland JBuilder tool, and
for you to write, compile, and execute a simple Java application that displays the message
"Hello world!".
Start by loading the Borland JBuilder tool. JBuilder remembers the most recent project
you've worked on, and automatically opens it for you. Since we're creating a new project,
however, we need to close it and create a new project.
From the 'File' menu, select the 'Close All' menu option.
From the 'File' menu, select the 'New Project' menu option
JBuilder will now display a Project Wizard. This wizard will ask you for information
about your project, and then generate a project file for you.
Enter as the file name for your project "MyFirstProject.jpr". It's best to place
projects in their own directories. You can also click the browse button to use a file
selection dialog box.
Enter as a title 'My First Project', your name, and a brief description.
Click 'finish'.
Figure 1.0 - Project Wizard prompts you for details about your project
Figure 2.0 - Project view allows you to select, add and remove files
To write an application, we'll need to create a new source file, which we'll call
"HelloWorld.java".
Click on the 'Add to project button' (the blue folder with a green plus icon).
As a filename, use 'HelloWorld.java'.
Click on the 'Open' button to create the file.
Up until now, we haven't had to write any code. Now its time to get your hands dirty.
Start by typing the following code into the "HelloWorld.java" file from Step Two.
package MyFirstProject;
Don't worry if you're a little unsure about what the code does. We'll examine the code line
by line.
1: package MyFirstProject;
This line states that the class belongs to the package MyFirstProject. A package is a
collection of Java classes and interfaces. We group logically related code together in a
package. As a general rule, each application should have its own unique package. Use the
same name for your package as you do for your project.
This line declares a new class, called "HelloWorld", and that it is publicly accessible. A
class is composed of data variables (members), and functions (methods).
All applications written in Java share at least one thing in common - they all have a main
method. You must declare a public static main method that accepts command line
parameters (args), for your application to run. Our main method also declares that it could
generate an I/O error when reading input from the user.
This is where our application writes a message to the user. System.out is an object that
allows us to write to "standard output", which is the user's console.
5: System.in.read();
So that you can read the message before the application terminates, we need to wait for
keyboard input. System.in is an object that allows us to read from "standard input", which
is the sequence of characters entered by the user.
Before you can run an application, you must first compile it. A compiler takes source
code (the instructions for a program), and converts it into executable instructions (a
program that can be run). We'll compile the HelloWorld application now.
From the 'Build' menu, select the 'Make "HelloWorld.java" menu item.
If you've typed in the code correctly, your application will now be ready to run. If not,
carefully check your code again, and repeat.
Once the application is compiled, its ready to run. JBuilder runs applications in their own
window, which you'll see in a moment.
From the 'Run' menu, select the 'Run "HelloWorld" menu item, or use the shortcut
of shift-F9
The HelloWorld application will then appear in a new window. You'll see the message
"Hello World!", and the program will wait patiently until you press the <ENTER> key.
Summary
Overview
In this lesson, you'll learn how to write simple applets in Borland JBuilder, and how to
use the design view to construct a graphical user-interface (GUI). An applet is an
executable program that runs inside a browser, such as Netscape or Internet Explorer.
We'll create a simple applet, and learn how they work.
From the 'File' menu, select the 'New Project' menu option. Enter a filename, a
title, and your name into the Project Wizard dialog box. Click 'Finish'.
Next, from the "File" menu, select "Project Properties", and modify the output
path to our project directory.
An applet is a class that is executed under the control of a world-wide-web browser, such
as Netscape Navigator or Internet Explorer. We'll create a new class, called AppletDemo,
that runs as an applet.
From the 'File' menu, select the 'New' menu item.
Select the 'Class' option, and click OK.
Figure 1.0 - New object dialog allows you to insert a new class into your project.
After clicking OK, the "New Object" dialog box will appear. This allows you to
customize the class details. Enter 'AppletDemo' in the name field, and 'java.applet.Applet'
in the extends field. This tells JBuilder that our new class will inherit all the properties of
an applet. By default, JBuilder will also put your new classes in the current package - but
for the purpose of this tutorial, remove the package declaration. Leave the other fields as
they are for now, and click OK.
Figure 2.0 - New object dialog allows you to insert a new class into your project.
Step Three - Enter the code
JBuilder automatically creates code for us, but will need to supplement it with out own.
Our applet will display a texfield (allowing users to type in a URL), and a button. When a
user clicks on the button, the web browser will load the page located at the URL.
Modify the source for AppletDemo.java, until it looks like the following listing, and then
build the project.
import java.awt.*;
import java.applet.*;
import java.net.*;
// Default constructor
public AppletDemo() {
}
repaint();
}
}
}
Once you've typed out the listing, and compiled it, your applet is almost complete. You'll
also need to create a new HTML page. Click on the 'Add to Project' button, to create a
new file. Call this file "AppletDemo.html".
Figure 3.0 - Project view allows you to insert new files into the project
Once you've created a HTML file, click on the 'Source' tab to edit it. Add the following
code to your HTML page :-
<hr>
<center>
<applet code="AppletDemo.class" height=40 width=200>
This applet requires a Java browser
</applet>
</center>
<hr>
Our applet is now ready to execute. Save all open files and then load the Netscape
browser. From the "File" menu, select the "Open page" menu item. Netscape will prompt
you to enter a location. Click on the "Choose File" and enter the location of your project
directory. Netscape will display a list of HTML files - choose Week2Applet.html.
The applet will now be displayed, and you should see the textfield and button. Enter a
URL, such as 'http://www.bond.edu.au/', and click "Go!".
Summary