GUI Applications

ninetimesdissemblingSoftware and s/w Development

Nov 10, 2012 (4 years and 5 months ago)

239 views

GUI Applications

Contents

A.
Labels, Text Fields, and Buttons

1.
Problem: The Hello Application

2.
Development Steps

B.
Option Panes

1.
New Requirement for the Hello Application: Using
an Option Pane

2.
New Requirement: Removing the Button and
Allowing the User to Press Enter

C.
Exercises

1.
Converting Kilometers to Miles

2.
The Number Guessing Game

A. Labels, Text Fields, and
Buttons

The Hello Application

1. Problem: The Hello Application


Develop a GUI application that asks the
user to input a name and then displays a
welcome message to that name

2. Development Steps

1.
Creating the Project

2.
Creating the View

2.1. Creating the GUI Container

2.2. Putting GUI Components into the GUI
Container

3.
Implementing an Event Handler for the
Button

4.
Running the project

1. Creating the Project


File > New Project…


New Project


Categories: Java


Projects: Java Application


Next


New Java Application


Project Name:
HelloDesktop


Turn off “Create Main Class”


Finish

2. Creating the View

2.1. Creating the GUI Container


Right
-
click the project name > New >
JFrame Form…


New JFrame Form


Class Name:
HelloView


Package:
hello


Finish


Naming the Form


title:
hello

2.2. Putting GUI Components into
the Container



Label


text:
Name:


Text Field


text:
Enter

your

name


Variable Name:
nameTextField


Button


text:
Say Hello


Variable Name:
sayHelloButton


Label


text:


Variable Name:
messageLabel

3. Implementing an Event Handler
for the Button


Double
-
click the
Say

Hello

button (or
right
-
click > Events > Action >
actionPerformed)

4. Running Application


Click the
Run

Main

Project

(F6)

button

B. Option Panes

1. New Requirement for the Hello
Application


Modify the application so that after the
user enters a name, the welcome
message will be shown in a different
window

Solution


2. New Requirement: Removing the Button
and Allowing the User to Press Enter


Modify the application so that after the
user enters a name and press the Enter
key, the welcome message will be shown
in a different window

Solution


Removing the Button


Implementing an Event Handler for the
Text Field


Double
-
click the text field

C. Exercises

1. Exercise: Converting Kilometers to Miles


A kilometer is equal to 0.6214 miles.
Develop a GUI in which the user will enter
a distance in kilometers, and then click a
button to see the distance converted to
miles

Solution


2. Exercise: The Number Guessing Game


Develop an application that asks the user
to input a guess number (See the next
slide for an example). After the user enters
a number and clicks the Submit button, the
application displays one among the
following messages


Sorry, try something higher


Too bad, go lower


Sweet, you got it right!


Development Steps

1.
Creating the View

2.
Generating the target number

3.
Adding functionality to the button

3.1. Validating the input

3.2. Matching the guess number to the target

1. Creating the View


2. Generating the Target Number


3. Adding Functionality to Button

3.1. Validating the Input


3. Adding Functionality to Button

3.2. Matching the Guess Number to the Target


References

1.
GUI Building in NetBeans IDE 5.5.
http://testwww.netbeans.org/kb/55/quickstart
-
gui.html

2.
Developing a Web Application.
http://testwww.netbeans.org/kb/55/vwp
-
helloweb.html

3.
Adding Functionality to Buttons: A Beginner Guide.
http://testwww.netbeans.org/kb/articles/gui
-
functionality.html

4.
Starting Out With Java 5 from control structures to
objects. Tony Gaddis

5.
Introduction to Facelets.
http://testwww.netbeans.org/kb/55/quick
-
start
-
facelets
-
in
-
netbeans.html