Introduktionsovelse til Java-programmering

searchcoilΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 11 μήνες)

238 εμφανίσεις




Side
1

af
10




COMPUTER SUBJECT:

BASIC JAVA
-
GUI

PROGRAMMING


TYPE:

GROUP WORK EXERCISE/DISCUSSION


IDENTIFICATION:



INTRODUCTIONGUI
/MC


COPYRIGHT:




Michael Claudiu


LEVEL:

EASY


DURATION:

2 hours


SIZE:

5

lines!!


OBJECTIVE:

Use of
Netbeans

and understand
ing the composition of a
Java
-
program.


REQUIREMENTS:

BigJava

Ch.

2.2.3
-
2.8, 2.10


COMMANDS:

Class <class name> {






}


jTextField1.setText("Welcome to GUI
-
Java");


import java.util.Date;












Side
2

af
10

Introduction to Java
GUI
-
programming


Purpose:



The purpos
e with this exercise is to give an introduction in writing, compiling, adapting an
d
running a simple J
av
a
-
program using the NetBeans 5.0

editor.

We assume that you are
aquatinted to Windows and the pathfinder (
Windows
explorer/ in Danish:
stifinder).


Prec
onditions:



”Java
2 SDK Standard Edition vs.1.5
” and ”JAVA runtime environment” must be installed



The ”
Netbeans

-
editor must be installed in a way that makes it possible to activate
the

Compile

-

and ”
Run

-

commands
.

1.

Use the "
Pathfinder
"
(
“Explore”
)
to c
reate a folder named "
SW
-
Construction
" for your *.java
documents, to avoid clutter. It should be created in your per
sonal drive on the network (H:
\
)

. Or
it can be created on your memory stick.
Here you can save all your programs. If you are smart
you actu
ally
first
create a folder named

SW
-
Construction


and then a subfolder named
"
Solutions
".


2.

Start the NetBeans
-
editor,
used to

write and edit the Java source
-
code before it’s compiled and
to adapt and edit it afterwards.
Click on the NetBeans
6
.0

icon OR
Ch
oose

Start


and Select
Programs
-
>
NetBeans
6
.0

-
> Netbeans

IDE
.


3.

You will then get the following
window:





Projects for all coming

Java projects





Side
3

af
10


4.

Create

a new project in the folder/directory. . Choose
“File”

-
> “New project
”.


5.

Then you will see the following
window where the

Project Category


and “Type” must be
defined:




Your first project category is “General” and the project is a standard “Java Application”.

Therefore Select:

“General


-
> “Java Application”
-
> “Next”.




Side
4

af
10


6.

Then
you will see the following

wi
ndow, where the “Project Name” and “Location” must be

defined, so

The editor knows the location of your (coming) Java
-
files in the project:





In
Project Name

write “Normal”

Use
“Browse”

to define the
Project Location

to
be
one of the following:


IF you

are on wireless laptop and using memory stick (USB drive).

“D:
\
SW
-
Construction on the D
-
drive


IF you are on wireless laptop and using C
-
drive

“C:
\
Documents and Settings
\
YourUsername
\
SW
-
Construction on the C
-
drive,


IF you are on fast net PC using your
personal H
-
drive.


“H:
\
SW
-
Construction
\
Solutions on the H
-
drive,


Click on
“Finish”








Side
5

af
10

7.

Now the project is created and
the following window pops up:




As you can see a class Main is automatically generated.



8.

We are now ready to create a file for you
r first GUI
-
program.

Click on your
folder

for Java files
normal
, then:

Select
File
-
> New File
-
>
Swing
GUI Forms
-
> JFrame Form

G
ive the name
WelcomeGUI

in the <default name> textbox.

To submit Select:
Next


Your
folder for Java files




Side
6

af
10


9.

Now you will see the following:







10.

We are now ready to Compile the program.

Select
Build
-
> Compile

Alternatively just Press F9.


11.

We are now ready to Run (Execute) the program.

Select
Run
-
> Run File
-
> Run “WelcomeGUI.java”


Alternatively just Press Shift + F6.


You should get an empt
y window; close it.


12.

Change the Layout t
o AbsoluteLayout for the JFrame

Select
JFrame
in the Inspector window
,


Clic
k on the right

mouse
button

and choose


S
et Layout
-
> AbsoluteLayout

Compile and run again, now the window is very small…. .






Your

Design

Window

ChangeLayout

Palette with e
lements like
buttons and text

fields for the
GUI
.

Layout formats etc.




Side
7

af
10

13.

Now we wi
ll use the Palette to insert a JPanel.

Under the elements scan the templates.

Find and Click on
Panel
.

Don’t hold the mouse button down !!!

Move mouse to
Your
Design
Window

and Click again.

Resize the
J
Panel to fill out the JFrame.

Compile and run again
.

S
ee the difference.


14.

Now we will use the Palette to create a JButton

Under the elements scan
the templates.

Find and Click on
Button
.

Don

t
hold

the mouse button down !!!

Move mouse to
Your
Des
i
gn
Window

and Click again.


Then you will see the following win
dow:






Button Identificatio
n

Your Button

with front
text

Button Color

and other
properties

Source with
Program code




Side
8

af
10


15.

Change the JButton’s identification
“jButton1”
(
a iden
tification is the variable name
) to

w
elcomeButton


by clicking
on JButton1 in Inspector and typing the new name.

Alternatively
Click on right mouse click and choose

Change variable
name
.


16.

Change the Text
(which is jButton1)
to Welcome

by
double
clicking on the
jButton1 in the
Design window.

Look at the Button properties and find Text.


17.

Change the background color of the WelcomeButton
to B
lue by clicking on the background
field in Pro
perties

and c
hoosing from the
Color
Palette.


18.

We are now ready to Compile the program.

Select
Build
-
>

Compile

Alternatively just Press F9
.


19.

We are now ready to Run (Execute) the program.

Select
Run
-
> Run File
-
> Run “WelcomeGUI.java”


Alternatively ju
st Press
Shift
+
F6
.



CONGRATULATIONS YOUR FIRST GUI
-
PROGRAM IS RUNNING



20.

Let us try to rename the class name

to WelcomeToMyGUI.

Select WelcomeGUI class in Source packeges.

Click on the right mouse
and Select
-
> Refactor
-
> Rename
.

Compile and run again.



Remark: Unfortunately the program is not doing anything at all !!!





Side
9

af
10

Assignment 1

Your missi
o
n is now to change the program

so

the text:


Welcome to
Java
GUI



is shown in a JTextField

when the WelcomeButton is c
licked
.


21.

Now we will create a JTextField.

Under the
template
elements scan the
GUI
-
templates
.

Find and Click on
JTextField
, Move mouse to
Design
Window

and Click again.


22.

We are now ready to
change the program code
.

There are several 3 way
s to do this and you must investigate
two of them now and on
e later
.


A.

Click on WelcomeGUI
Source
holding the program code

Click on right mouse and
Select
-
> Code Folds
-
> Expand all Java Code

Find the method
private v
oid w
elcomeButtonActionPerformed


Change the code into the following


private void w
elcomeButto
nActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:


jTe
xtField1.setText("Welcome to
Java
GUI
");


}


B.

Alternative

Select the welcomeButton in the Design window.

Click on right mouse and Select
-
> Event
s
-
> Action
-
> actionPerformed

Then the code automatically pops up.


C. Alternative

Double click somewhere, but where ..find out yourself later !!



23.

Activate the WelcomeGUIForm.

Compile and Run

!


24.

It would be better if the jTextField started with empty box
.

Do this.


Assignment 2

Your mission is now to change the program

so

the text:


Program started at

<Time of running>



is shown in the
Output Window

when program is executed..






Side
10

af
10

25.

Click on WelcomeGUI
source
holding the program code

Find the method
pri
vate void
main.

Change the code into the following.



public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {



public void run() {

new WelcomeGUI().setVisible
(
true
)
;

String date = new Date().toString();

System.out.
println("
Program started at
: "+date);

}


}


26.

Try to
Compile and Run

!

Unfortunately this gives an error
.
The c
lass

Date cannot be recognised!!!


Therefore in the beginning of the program just before


public class WelcomeGUI extends javax.swing.JFrame


add
the following sentence:


import java.util.Date;


Try to
Compile and Run

!

YEAH We are up and running.



Assignment 3

C
hange the program

so
the text:


Button clicked at <Time of running>



is shown in the

Output Box
each time
when the WelcomeButton is ac
tivated.



Assignment 4

Now
copy this
program in
to

another file named WelcomeGUI2.

Then fool around an
d add more elements and actions in the new

……



Congratulations. You have
accomplished the JavaGUI
-
programming mission





SO FAR SO GOOD