CS/SE 2430 - Program 0 - Introduction to Java and ...

weaverchurchSoftware and s/w Development

Aug 15, 2012 (4 years and 10 months ago)

206 views

CS/SE 2430
-

Lab

0
-

Introduction to Java and NetBeans 4.1


1.

Start up the NetBeans 4.1 IDE.


2.

T
o Create a Project:


Do
:

File/New Project
-

General Category, Java Application
, then
click next


For project Name, put

Lab0

For Project Location,
browse to:

S:
\
Courses
\
CSSE
\
clifton
\
243
\
<your_login_name>


Uncheck "Set Main as Project"

and
Uncheck "Create Main Class"


Click Finish


3.

To Make a GUI Frame:


Do
:

File/New File
-

Java GUI Form
s

Category, JFrame Form
, then
click next


For ClassName, use MyFirstFrame


For Location, use Source Packages


Click Finish


4.

Changing Properties

In the Inspector pane (lower left), select JFrame


R
ight Click, Select Layout, Absolute Layout

In the Properties Pane (lower right) are other properties

(
s
elect JFrame in Inspector pane)

For
T
itle, put: My First Java Program!



5.

Changing
C
ode

Click the Source Tab (near the top, down a few lines)

You will see all the code that was automatically generated.

It does a pretty good job of telling you what not to touch!

Change @author to your

name

Add the following code right after "initComponents();" in "public MyFirstFrame()":




setSize(400,

400);


6.

Build & Run

Right click on
Lab0

in Projects Pane and select “Set Main Project”

Do Build

/

Build Main Project

Run / Run Main Project: It will

ask to select
a
main class

Or you can Run / Run File
if MyFirstJava.java is highlighted in Projects pane


7.

Adding Components

Click back to the Design Tab

and
Use the Palette pane in the upper right

Click JTextField, then click on the "Frame" (middle pane)

The textfield should appear on the frame

Right click on jTextField1 in the Inspector, select

rename, the
n

type in txtNum1

Change the Text property (Lower right

pane
) to: 100

Click JButton, then click on the "Frame"

and r
ename it to btnOutput

Change the T
ext property to:
Output Number
!

D
rag them
around and change their sizes (this

changes X, Y, Width, Height properties
)

8.

Adding button handlers

D
ouble click on btnOutput button (on the Frame or Inspector)

It makes the handler
, takes you to the code,

and ind
icates where to type the code.

Type in: System.out.println( txtNum1.getText() );



9.

Build and run your program. You should be saving periodically.

When you click the button, the data in the text field gets

written to the Output pane.

You can make th
e Output pane bigger and

close the
I
nput pane.


Call me over and show me when you are done
.


Finished early? If yes, try:


A.

Add a Label over the textfield with text: "Enter Num 1"

B.

Add Another Text Field, txtNum2
,

a
nd label for it: "Enter Num 2”

C.

Add ano
ther Button
, btnAdd,

with Text of "Add"

D.

Make the handler for the
Add
button do:

int num1 = Integer.parseInt( txtNum1.getText() );

int num2 = Integer.parseInt( txtNum2.getText() );

int ans = num1 + num2;

System.out.println( "Answer: " + ans );

Note what hap
pens if you don't have integers in the textfields

and click the Add button.

Call me over if you finish this.


To Re
-
Open a Beans Project
:

Do F
ile/Open Project
,

t
hen browse to the folder, for
example:



S:
\
Courses
\
CSSE
\
clifton
\
243
\
jonesk
\
Lab0

and c
lick "Open Project Folder".


Want NetBeans 4.1 on your own machine?


Browse to:
http://java.sun.com/j2se/1.5.0/download
-
netbeans.html

Then download:
JDK 5.0 Update 4 with NetBeans IDE

4.1 Bundle

It is a little over 127 Megabytes.


Note that this does not include the JDK help.

You need to download and install that separately.

It is at:
http://java.sun.com/j2se/1.5.0/download.j
sp

It is:
J2SE 5.0 Documentation

It is about 43.5 Megabytes.

Download and install it.

To “Add” it into NetBeans, in NetBeans do:


Tools / Java Platform Manager


Click on the Javadoc Tab


Click Add / Zip Folder


Browse to the folder where you installed it
, for example:




C:
\
Program Files
\
Java
\
jdk
-
1_5_0
-
doc
\
docs