Fundamentals of Computers - Papademas.net

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 7 months ago)

114 views




NSA 2
1
0




Introduction
to Progra
m
m
ing
Using Java


Special Project 2


Student Name

Section


Instructor

Due Date








Thoroughly
read the objectives, instructions and requirements of the given packet and then use
suitable electronic technology tools to solve the projects. Submit both your program source
code, with your name, date and course information in the heading portion of your code, as well
as the required output(s). Design your application professionally and code it efficiently.




Project ( Creating a GUI Application )
100 Points
____
score

Objective
To create a Java - base graphical user interface ( GUI ).



PROJECT DESCRIPTION


The program code given in Figure 2 , generates the graphical user interface given
in Figure 1 below. The interface consists of a label field, a text field and a button.
The user of the interface will enter a number in the text field and, upon click of the
button, the number that the user typed in the field will be displayed in a message box.


Figure 1




Information About This Project

This program illustrates an example of the power of a graphical user interface designed
using Java
swing
components.



Steps To Complete This Project

STEP 1 Open JCreator and Write the Code

Open JCreator. From the JCreator main menu, click
F
ile and choose
N
ew… .
When the New window opens, click the Files tab. Within the Files tab and with
the Java File icon selected, click the
F
ilename field and enter the appropriate file
name. In this case, use the file name GUIExample1 since this is the class
name for this project. Click OK to close the New dialog box. When the
JCreator editor opens, type the program code shown in Figure 2 , which follows,
exactly as it appears, except substitute your own name in place of Sammy
Student.

The code basically creates a
JLabel
, a
JTextField
and a
JButton
object.
The
pane.add()
method is used to add these components onto the
JFrame
.

The code in Figure 1 merely creates the graphical user interface and does not
contain any code which will respond to a click event associated with the Enter .
button. You will modify this code later to add the action event to the button.







© Copyri
ght
2005 by P
.
E.
P
.



Page 1 of 4



NSA 2
1
0




Introduction
to Progra
m
m
ing
Using Java


Special Project 2


Student Name

Section


© Copyright 2005 by P.E.P.
Page 2 of 4
Project
( Creating a GUI Application )




Figure 2 Program Code for the GUI Example





//Sammy Student: programmer
import java.awt.*; //to use class Container
import javax.swing.*; //to use swing components.

class GUIExample1 extends JFrame
{

//class definition, which inherits from class JFrame

//define interface components
JLabel numberLabel;
JTextField numberTextField;
JButton displayButton;

public GUIExample1()
{
//class constructor will "construct" the user interface
//components to a content pane are placed in the order
//that they are added
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
//The getContentPane() method sets up the content pane.
//The FlowLayout manager lays out the components in a
//free flowing, left to right order.
numberLabel = new JLabel("Enter a number:");
numberTextField = new JTextField(4);
displayButton = new JButton("Enter");

pane.add(numberLabel);
pane.add(numberTextField);
pane.add(displayButton);

//The add() method adds components into the content pane.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("GUI Example");
pack();
show();
}

public static void main(String[] args)
{
new GUIExample1();
}
}











NSA 2
1
0




Introduction
to Progra
m
m
ing
Using Java


Special Project 2


Student Name

Section


© Copyright 2005 by P.E.P.
Page 3 of 4
Project
( Creating a GUI Application )




STEP 2 Compile, Execute and Test the Program

After you have typed the given program code, build and compile your program.

Once you have successfully compiled your program, run your program by
selecting
B
uild on the main menu and then clicking Execute F
i
le .


The interface below should then appear.




Close the frame and return to your Java text editor ( JCreator ).

STEP 3 Modify the Application

You will now add an action event to the Enter button. To do this, follow
carefully each of these instructions.

• First add these two
import
statements, and their associated comments, to the
heading portion of your code.


import java.awt.event.*;
//to allow use of the ActionListener



import javax.swing.JOptionPane;
//to allow use of a message box

• Locate the statement shown below.


class GUIExample1 extends JFrame





Modify the statement such that it will
implement
the
ActionListener
.
lass GUIExample1 extends JFrame implements ActionListener

c


• Locate the statement shown below.


pane.add(displayButton);




Directly below this statement, insert the following line of code.
isplayButton.addActionListener(this);
d


• Locate the block of statements shown below.


public static void main(String[] args)

{

new GUIExample1();

}



Directly above these statements, insert the following block of code.








NSA 2
1
0




Introduction
to Progra
m
m
ing
Using Java


Special Project 2


Student Name

Section


© Copyright 2005 by P.E.P.
Page 4 of 4
Project
( Creating a GUI Application )






public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();

if (source == displayButton)
{
double result = Integer.parseInt(numberTextField.getText());
JOptionPane.showMessageDialog(null, "you entered " +
(int)(result), "Hello", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}


STEP 4 Modify Your Program Code

The next step is to perform a modification to the code. Modify the program
code such that when the user enters a non - numeric character, the following
message box will be displayed.



When the user clicks OK to close the above message box, he / she will then
return to the application with another chance to enter a proper numeric quantity.

After adding the modification, run your program and observe the action of your
program when a non - numeric character is entered.

STEP 5 Submit Hardcopy of your Completed Project

Take screen snapshots of the operation of your modified GUI application.
Submit your screen snapshots and your complete program code for credit.