Title: GUI
1GUI
- GUI programming consists of creating objects by
which a user can effectively communicate with the
underlying application program and effect its
state and behavior. - The user must be able to understand the
interface, its functions, its images, and its
logical flow.
2GUI Effectiveness
- The users background, knowledge, experience, and
perspective define a usage context. - Different categories of users will have different
expectations. - The GUI should clearly convey the proper
perspective and level of detail appropriate for
the background of the target audience.
3GUI Principles
- When designing a GUI application, address the
users need first. - The application should be comprehensive,
meaningful, and appealing to the target audience. - The software design should support an interface
with the most meaningful abstractions, and be
intuitive for the user.
4GUI Tools
- C with MFC, OWL, Borland Component Classes,
Motif (Unix), etc. - Visual Basic
- Tcl/Tk
- Java with JFC
- Others.
5Tools (Cont.)
- The tools provide implementations of visual
components that represent familiar objects with
which the user interacts - The software engineer creates visual abstractions
of the interface using elements such as dialogs
buttons, text fields, etc., as building blocks
6General Architecture for GUI Applications
- The software engineer must work with two
orthogonal sets of abstractions - The application logic
- The application interface to users
- The challenge to the software engineer is to
bridge these abstractions so they can work
together.
7GUI Client/Server Model
8Javabook Classes
- This is the first class package that we will use
for developing simple to semi-complex GUI
applications - For the second part of the course, we will use
the AWT and an introduction to the Swing package.
These two sets of classes belong to the JFC.
9Using the javabook Package
- Includes predefined classes for GUI applications
- Promotes the principle that a Java programmer
should first learn to use library classes before
developing his/her own classes - Emphasizes learning OO programming principles
rather than the Java language.
10First Sample GUI Program
- Develop a program that displays a short message
in a small dialog box. - After searching the javabook package for suitable
classes for implementing the problem, two
predefined classes are identified - MainWindow
- MessageBox
11First GUI Program (Cont.)
- The two objects that will be created are
- the frame window called mainWindow
- the dialog box called messageBox.
- The MessageBox class is used to implement a
dialog box. This is a small window with a short
message. The dialog box is subordinated to the
frame window.
12Java Source Code
import javabook. class DisplayMessage
// the main class for this
application public static void main (String
args) MainWindow myWindow //
declare frame MessageBox messBox //
declare message box object // myWindow
new MainWindow(Kennesaw State) messBox
new MessageBox (myWindow) //
myWindow.show() messBox.show(I am happy
learning Java)
13Compile and Execute a program in Java
To use only the System Java packages cgt javac
myprog.java cgt java myprog To use the
javabook package cgt javac -classpath c\java
FunTime.java cgt java -classpath c\java .
FunTime To execute an applet cgt appletviewer
HelloDemo.html
14General Program Structure in Java
- Comments - document purpose, inputs outputs,
limitations, date, author, etc. - Access to predefined classes (library classes in
packages) - Define the main class
- Declare objects
- Create objects
- Manipulate objects
15Access Predefined Classes
- Need to access class packages (import statement).
- A package is a group of related classes
- Packages can be
- System Java packages
- Package of user-defined classes
16The Main Class
- The programmer normally defines one or more
classes, one of which is the main class. - The main class must include the definition of the
of method main.
17Java Applets
- A small application that runs with a Web browser
- Can be tested with the Java utility appletviewer.
- The applet is run from an HTML file
18Source Java Code for an Applet
import java.applet. import java.awt. public
class Myapplet extends Applet public void
paint (Graphics mygraph)
mygraph.drawString (I am working in Java)
mygraph.drawRect(50, 50, 100, 30)
19HTML file for the Applet
ltHTMLgt ltBODYgt ltAPPLET CODE Myapplet.class
WIDTH310 HEIGHT 200gt lt/APPLETgt lt/BODYgt lt/HTMLgt
20Variables and Objects
- Primitive data types are the ones used in
elementary variables (of type int, float, char,
etc.). Variables of primitives do not need to be
created. - Reference data types are the ones used to declare
objects. These are the classes of the objects.
21Casting
- Implicit - when used in expressions that include
variables of various types - Explicit - when a value of a variable is forced
to change to a target type. - Y (float) x / 5
- Pobject x (Pobject) fulltime(y)
22Math Class
- Mathematical functions and constants are
accessible from the Math class as static members. - y x Math.sin(x)
- y Math.PI Math.asin(x)
23Reading Values with InputBox
- An InputBox object is a dialog
- It needs an owner frame, an object of MainWindow.
- MainWindow myWindow
- InputBox inputval
- inputval new InputBox(myWindow)
- To input an integer
- int y inputval.getInteger(Enter count)
24Display Values with OutputBox
- An OutputBox object is a dialog
- It needs an owner fram object of class
MainWindow. - MainWindow myWindow
- OutputBox outputval
- outputval new OutputBox(myWindow)
- To display a value
- outval.printLine(Length y inches.)
25Instantiable Classes
- Instants (objects) of the class can be declared
and create - Need to be defined when no predefined classes are
available for the problem solution - The class specification (interface) depends on
the class requirements and how we are going to
use the class
26Class Members
- Visibility (accessibility) modifiers
- public
- private
- protected
- Data members - should always be declared private,
to ensure integrity of the class - Methods
- several methods should be defined, some of which
are the public accessor methods for the private
data members.
27Instance and Class Members
- An instance data member will be owned by a
particular object, when created - A class data member is shared by all objects of
the class - Class members need to be declared with the
static modifier - To access class members, the name of the class is
specified, then the member name.
28Class Constructors
- A special function (method) needed to initialize
an object to a valid state. - Has no return type and has the same name as the
class - Are normally overloaded when more than one way to
initialize an object is needed - Different number of parameters
- Different types for the parameters
29Scope for Variables and Objects
- Data members - accessible from all the member
methods in the class - Global data - accessible from any other class.
Not allowed in Java - Local data - accessible only from within the
method
30Persistency of Data
- Exist permanently
- Exist throughout the life of the complete program
- Exist throughout the life of the object to which
they belong - Exist only while the enclosing method executes
31Applets with GUIs
- Access the applet class
- Access the awt package -- The Abstract Windowing
Toolkit - import java.applet.
- import java.awt.
- These applets will normally need to declare and
create GUI objects from the awt package
32Placement of GUI Objects
- After creating the GUI objects, they are placed
on the viewer window - The default placing is left-to-right and
top-to-bottom (the FlowLayout layout manager)
33GreetingApplet
- This applet uses
- Two Label objects (prompt and greeting)
- One TextField object (inputLine)
- After declaring these objects, they need to be
created, then they need to be associated with the
applet by invoking the add method to make the
objects visible.
34Applet Constructor
public GreetingApplet () prompt new Label
(Type your name ) greeting new Label()
inputLine new TextField (20) // now make
these objects visible add(prompt)
add(greeting) add(inputLine)
35Events
- An event is an instantaneous action
- Pressing a key
- Clicking a button on the mouse
- Selecting a menu item, etc.
- We need the program to respond to such events by
executing some routine(s) - Such interfaces are said to be event-driven
36Listener Objects
- The Java event model is based on the concept of
listeners. - A listener is an object whose sole purpose is
- To wait for an event to occur
- When the event does occur, the listener performs
(carries out) the appropriate behavior.
37Event Types and Objects
- There are two types of objects related to events
- event source (generates events)
- event listener (detects events)
- Action events are one of several type of events
- ENTER key event
- Mouse movement event
38Applet as a Listener Object
- The applet responds to the action event generated
by the inputLine object. The applet definition
must include - Import the Java event-handling package
- Implement the ActionListener class
- Define the method that will respond to the action
event generated - Register the applet object to the event source
object (inputLine)
39GreetingApplet
/ Program GreetingApplet An applet that
accepts the user's name and displays a
personalized greeting. / import
java.awt. import java.applet. import
java.awt.event.
public class GreetingApplet extends Applet
implements ActionListener
/ Data Members
/ private Label
prompt private Label greeting
private TextField inputLine
40GreetingApplet (Cont.)
/ Constructor
/ public GreetingApplet
() // Create GUI objects prompt
new Label("Please enter your name")
greeting new Label() inputLine new
TextField(15) // add GUI objects to
the applet add(prompt)
add(inputLine) add(greeting)
// add this applet as an action listener
inputLine.addActionListener(this)
41GreetingApplet (Cont.)
/ Public methods void
actionPerformed( ActionEvent ) Method
actionPerformed Purpose Implements the
abstract method defined in the interface
ActionListener. The method retrieves the
text from the TextField object
inputLine and displays the personalized
greeting using the Label object greeting
Parameters ActionEvent object
Returns none / public void
actionPerformed(ActionEvent event)
greeting.setText("Nice to meet you, "
inputLine.getText() ".")
add(greeting) doLayout()
42The Button Class
- One of the most common GUI objects used today is
the pushbutton - To declare and create an object of class Button
- Button mybutton
- mybutton new Button(Ok)
- A button generates an action event when we click
on it.
43Registering a Button Object
- In addition to pressing the ENTER key, the applet
will also listen to the mouse click on a button.
The response will be the same. - To register the applet to both source objects
inputLine and mybutton, we need the following
statements in the applet constructor - inputLine.addActionListener(this)
- mybutton.addActionListener(this)
44Wrapper Classes
- Used to convert the primitive type values to
objects - Example
- String mystr
- Integer a new Integer (12)
- mystr a.toString()
- Float x
- x new Float (45.6)
- Float y new Float (45.6f)
45Primitive and Wrapper Types
- All collection classes in the Java library
maintain variables of type Object. - Primitive types cannot be directly stored in
these collections - When variables are removed from the collection,
they must be cast back to their original type - An object of a wrapper class can be stored in a
collection
46Menus
- Users can select one of several choices
- The interface provides a list of valid choices
- The javabook package includes the ListBox class
for implementing menus - Declare an object of class ListBox
- Create the object with a label
- Add the names of the options
- Invoke the method getSelectedIndex of the ListBox
object
47Selection in ListBox Object
- When the user clicks on the appropriate
selection, the getSelectedIndex method returns an
integer value - The integer represents the index value of the
selected choice. The value is a zero-based index. - selection menuObject.getSelectedIndex()
- Use the switch statement to process the index
value.
48Selection Example
// Partial code for selection task MainWindow
mywindow ListBox mychoices
// object for listing choices int myselection
// index value of
selection // mywindow new MainWindow mychoices
new ListBox (mywindow, Student
status) // mychoices.addItem(Freshman) mychoi
ces.addItem(Sophmore) mychoices.addItem(Junior
) mychoices.addItem(Senior) myselection
mychoices.getSelectedIndex() // // process
selection switch(myselection) .
49The Format Class
- Another GUI class from the javabook pakage
- This class has static methods for alignment and
assignment of spaces (field width) for values
displayed - Format.ltalignmentgt(ltfieldwidthgt, ltint expgt)
- Format.ltalignmentgt(ltfieldwidthgt, ltdec placesgt,
ltreal expgt)
50Using Format
int length float expense outputBox.printLine(For
mat.leftAlign(8, length)) outputBox.printLine(
Format.rightAlign(10,2, expense))
51ResponseBox Class
- Another GUI class from the javabook package
- An object of this class gets a yes or no
response from the user - MainWindow mywin MainWindow()
- ResponseBox yesno new ResponseBox(mywin)
- int myselect / /
for selection index - myselect yesno.prompt (Do you want to
continue?)
52Labeling the Buttons
- To label the buttons, use the setLabel methos of
class ResponseBox - ResponseBox mysel ResponseBox(mywin,2)
- mysel.setLabel(ResponseBox.BUTTON1, In-state)
- mysel.setLabel(ResponseBox.BUTTON2, Out-state)
- choice mysel.prompt(Residence status?)
-
- if (choice ResponseBox.BUTTON2)