CS/SE 2430 - Lab 1 - Introduction to Java and NetBeans 6.9

hundredhonkSoftware and s/w Development

Aug 15, 2012 (5 years and 2 months ago)

302 views

CS/SE 2430
-

Lab

1

-
Introduction to Java and NetBeans
6
.
9


1.

Start up the NetBeans
6
.
9
IDE.


2.

T
o Create a Project:


Do
:
File/New Project


Java Category /
Java Application
, then
click next


For project Name, put

Lab
1

For Project Location,
browse to:

S:
\
Co
urses
\
CSSE
\
summerss
\
243
0
\
<your_login_name>


Uncheck "Set
as
Main Project"
and
Uncheck "Create Main Class"


Click Finish


Let me know if you don’t see a folder with your login name.


3.

To Make a GUI Frame:


Do
:
File/New File
-

Swing
GUI Form
s
Category, JFram
e Form
, then
click next


For ClassName, use MyFirstFrame


For Location, use Source Packages


Click Finish


4.

Changing Properties

In the Inspector pane (lower left),
click on
JFrame

to select it

In the Properties Pane (lower right) are properties

for
JFrame

For
T
itle
, put: My First Java Program!



5.

Changing
C
ode

Click the Source Tab (near the top
middle
, 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
Lab1
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 MyFirst
Frame
.java is highlighted in Projects pane

Or you can right click on MyFirstFrame.java

in Projects pane and select Run File


7.

Adding Components
(Note: there are multiple ways to do so
me of the tasks below)

Click back to the Design Tab
and
Use the Palette pane in the upper right

In the Swing Controls, c
lick TextField, then click on the "Frame" (middle pane)

The textfield should appear on the frame

Right click on
the
jTextField1, select

“Change Variable Name”
:
txtNum1


C
lick
on
the
jTextField1

and
change the text:
100


In the Palette, c
lick Button, then click on the "Frame"
, then
r
ename it to btnOutput

In the Properties for Button,
change
the Text property to:
Output Number
!

D
rag the
m
around and change their sizes (this
changes X, Y, Width, Height properties
)


8.

Adding button handlers

D
ouble click on btnOutput button

It makes the handler
, takes you to the code,
and indicates 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 the Output pane bigger
.


10.

Add more features

A.

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

B.

Add
a
nother TextField, txtNum2
,
a
nd label for it: "Enter Num 2”
, and put text of: 200

C.

Add another 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 happens if you don't have integers in the textfields
and click the Add button.


Call me over and show me when you are done
.



To Re
-
O
pen a Beans Project
:



Do F
ile/Open Project
,

t
hen browse to the folder, for example:



S:
\
Cour
ses
\
CSSE
\
summerss
\
243
0
\
jonesk
\
Lab1

and click "Open Project Folder".



Keyboard Shortcuts:


Help

> Keyboard Shortcuts gives a list of keyboard shortcuts. If a UW
P computer
ask
s
for a
path to a PDF reader:

C:
\
Program Files
\
Adobe
\
Reader 8.0
\
Reader
\
AcroRd32.exe
.

A few shortcuts you may find of use are: sout, psvm, ifelse, fori

Type in the shortcut followed by a
tab
and it generates some code. Speeds up typing!



W
ant NetBeans
6
.
9
on your own machine?


The
class share has a file called: NetBeans.txt that explains how to get it.

There will also be a link to this file on the 2430 web page.

Be sure to change the settings to match the programming ground rules!