How to Provide Basic Functionalities to a GUI using ...

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

15 Αυγ 2012 (πριν από 5 χρόνια και 8 μέρες)

212 εμφανίσεις

H
OW TO
P
ROVIDE
B
ASIC
F
UNCTIONALITIES TO A
GUI

USING
N
ET
B
EANS
IDE

These are the steps required to create a basic GUI with two buttons and one text area using the NetBeans
GUI Builder:

1.

Open NetBeans, go to
File

>
New Project
.

2.

Select
Java

in the
Categories

Li
st (the left
-
hand side).

3.

Select
Java Application

in the
Projects List

(the right
-
hand side).

Click
Next
.

4.

Enter the
Project Name
, for example
TimeAndQuitGUI
.

5.

Uncheck the
Create Main Class

box
.
Click
Finish
.

6.

Go to
File

and
New File
.

7.

Select
Swing GUI Forms
an
d then
JFrame Form
. Click
Next.

8.

Enter the
Class Name
, for example
TimeAndQuitGUI
. Click
Finish.

9.

W
e can see the
Design

View of our
JFrame Form
.

Now we are able to add the components onto the form. We do that using the Palette window (which is
usually locate
d at the right
-
hand side) simply by dragging a component from the Palette onto the form.
For this exercise we need 2 buttons (JButton) and 1 text area (JTextArea). There are two ways to rename
the text of buttons:

1.

Right
-
click on the desired button
;

select
Edit Text
;

type the new text and press
Enter
.

2.

Select the desired button (left
-
click); Press the
F2

key; type the new text and press
Enter
.


Once the components are laid out on the window, we can easily p
rov
ide the

functionalities

needed.

Let’s
begin by exp
laining how event handling works. Basically, an event is triggered through some kind of
interaction between the user and the GUI. Java Swing provides a bunch of events; for almost every
change on the GUI components throws (or triggers) an event. For exampl
e, when the user clicks on a
button, we have an ActionEvent or a MouseEvent. These “Events” are handled by Java methods which
take by parameter an event object (ActionEvent, for instance).
The event object contains useful
information to help the handler de
cide what to do next. In this application we do not need to handle the
event object. We just need to perform two simple actions: (1) quit the application (Quit button) and (2)
display the current time (Time button). Steps to add an event handler to button:

1.

Right
-
click on the
Time

button, select Events, Action,
actionPerformed.

2.

Within the body of the method which has been created, set the text of the text area using the
Date

cla
ss, from the
java.util

package, like this:


private void jButton1ActionPerform
ed(java.awt.event.ActionEvent evt) {


jTextArea1.setText(
new Date().toString()
);


}

This is the code for the Time button. To make the Quit button work, we need this code:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


System.exit(0);


}


Challenges encountered while doing this:

1.

The text area only holds String values; we always need to convert a value before display it in the text
area. Here I converted a Date value to a String value using the toString() metho
d.

2.

Sometimes the NetBeans GUI Builder is pretty annoying and frustrating, especially when we have a
lot of components to deal within the same JFrame. You try to do a thing but the GUI Builder does
another completely different from what you are expecting fo
r. But it is ok most of the times.