Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

JBuilder Lesson 1 - Hello World!

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!".

Step One - Creating a project

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

Step Two - Creating a source file


Once you've created a project, you can begin to write your first Java application. The
project view allows you to manage your project's files. You can select a file, and edit it, or
you can add/remove files using the 'Add to project' and 'Remove from project' buttons.
These can be easily recognised by the plus and minus signs on these buttons.

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.

Step Three - Writing your first application

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;

public class HelloWorld


{
public static void main(String args[]) throws Exception
{
System.out.println ("Hello world!");
System.in.read();
}
}

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.

2: public class HelloWorld

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).

3: public static void main(String args[]) throws IOException

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.

4: System.out.println ("Hello world!");

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.

Step Four - Compiling the application

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.

Step Five - Running the application

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

At the conclusion of this lesson, you should be able to do the following :-

1. Create a new project


2. Add files to your project
3. Write code to send messages to standard output
4. Compile code
5. Execute compiled code

JBuilder Lesson 2 - Introduction to applets

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.

Step One - Creating a project

Create a new project, called "AppletDemo", as explained in the previous lesson.


Remember the name of the directory where you create your project - we'll need this
information.

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.

eg - If your project name was "c:\JBuilder\myprojects\week2\week2.jpr", your


project directory is "c:\JBuilder\myprojects\week2\"

Step Two - Creating a source file

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.*;

public class AppletDemo extends Applet


{
// Member variables of applet
TextField urlField;
Button goButton;

// Default constructor
public AppletDemo() {
}

// Initialization code for applet


public void init()
{

// Create a text field of size 15 using the


'new' keyword
urlField = new TextField(15);

// Create a new button, labelled Go!


goButton = new Button("Go!");

// Add text field and button to our applet's


GUI
add(urlField);
add(goButton);

repaint();
}

public void browse(String url)


{
try
{
AppletContext ac = getAppletContext();
ac.showDocument ( new URL(url) );
}
catch (Exception e)
{

}
}

// Action handlers respond to user interface events


public boolean action(Event evt, Object obj)
{
String location = urlField.getText();
browse (location);

// Return true, since we performed an action


return true;
}
}

Step Five - Creating a HTML page to display applet

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>

Save your file, and you're ready to run the applet!

Step Six - Runing the applet

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.

Hint - If your project name was "c:\JBuilder\myprojects\week2\week2.jpr", your project


directory is "c:\JBuilder\myprojects\week2\"

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

At the conclusion of this lesson, you should be able to do the following :-

1. Create a new project


2. Modify your project's output path, to place your class files with your other project
files
3. Create a small applet
4. Create a HTML page, containing code to display an applet
5. Load a HTML page in Netscape from a local drive

You might also like