chapter 6

29
Chapter 6 Chapter 6 Graphical User Interface Graphical User Interface (GUI) and Object-Oriented (GUI) and Object-Oriented Design (OOD) Design (OOD)

Upload: karan

Post on 05-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Chapter 6. Graphical User Interface (GUI) and Object-Oriented Design (OOD). class JButton. Provided to create buttons in Java Used to create and modify JButton objects Button may be labeled with a text string or an icon May register a listener object to process button click - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6

Chapter 6Chapter 6

Graphical User Interface (GUI) and Graphical User Interface (GUI) and Object-Oriented Design (OOD)Object-Oriented Design (OOD)

Page 2: Chapter 6

class JButtonclass JButton

Provided to create buttons in JavaProvided to create buttons in Java Used to create and modify JButton objects Used to create and modify JButton objects

Button may be labeled with a text string or an iconButton may be labeled with a text string or an icon May register a listener object to process button clickMay register a listener object to process button click

To create buttonTo create button Declare reference variable of type JButtonDeclare reference variable of type JButton Instantiate objectInstantiate object

To add a text field to a container (display the label in To add a text field to a container (display the label in the content pane) use the method add from the the content pane) use the method add from the container classcontainer class

Page 3: Chapter 6

Methods Provided by the class JButtonMethods Provided by the class JButton

Page 4: Chapter 6

Methods Provided by the class Methods Provided by the class JButtonJButton

Page 5: Chapter 6

Handling an EventHandling an Event

Action event: event created when JButton is Action event: event created when JButton is clickedclicked

Event listener: object which receives Event listener: object which receives message when JButton is clickedmessage when JButton is clicked

In Java, you must register the listenerIn Java, you must register the listener Specify the listener object for a particular Specify the listener object for a particular

buttonbutton

Page 6: Chapter 6

Handling an EventHandling an Event

class ActionListenerclass ActionListener Handles action event Handles action event Part of package java.awt.EventPart of package java.awt.Event The class ActionListener is a special type of class The class ActionListener is a special type of class

(interface)(interface) Must contain actionPerformed method Must contain actionPerformed method

Page 7: Chapter 6

Rectangle Program: Rectangle Program: Sample RunSample Run

Page 8: Chapter 6

Programming Example: Programming Example: Temperature ConversionTemperature Conversion

Input: Input: temperature in Fahrenheit or Centigradetemperature in Fahrenheit or Centigrade Output: Output: temperature in Centigrade if input is temperature in Centigrade if input is

Fahrenheit; temperature in Fahrenheit if input Fahrenheit; temperature in Fahrenheit if input is Centigradeis Centigrade

Page 9: Chapter 6

Programming Example: Temperature Programming Example: Temperature ConversionConversion

Solution:Solution: Create the appropriate JLabels, JTextFields, Create the appropriate JLabels, JTextFields,

JButtonsJButtons Add them to the created content pane Add them to the created content pane Calculate the appropriate conversions when the Calculate the appropriate conversions when the

buttons are clicked and an event is triggeredbuttons are clicked and an event is triggered

Page 10: Chapter 6

Sample Run for TempConversionSample Run for TempConversion

Page 11: Chapter 6

Object-Oriented DesignObject-Oriented Design

Simplified methodologySimplified methodology1. Write down detailed description of problem1. Write down detailed description of problem

2. Identify all (relevant) nouns and verbs2. Identify all (relevant) nouns and verbs

3. From list of nouns, select objects3. From list of nouns, select objects

4. Identify data components of each object4. Identify data components of each object

5. From list of verbs, select operations5. From list of verbs, select operations

Page 12: Chapter 6

Object-Oriented Design Example: 1Object-Oriented Design Example: 1 Problem StatementProblem Statement

Write a program to input the length and width of a Write a program to input the length and width of a rectangle and calculate and print the perimeter and rectangle and calculate and print the perimeter and area of the rectanglearea of the rectangle

Select NounsSelect Nouns Length, width, rectangle, perimeter, areaLength, width, rectangle, perimeter, area

Identify ClassesIdentify Classes Length, width, perimeter and area are properties of Length, width, perimeter and area are properties of

a rectangle: a rectangle: Rectangle will be the classRectangle will be the class

Page 13: Chapter 6

Object-Oriented Design Example: 2Object-Oriented Design Example: 2 Select essential attributes for the object of type Select essential attributes for the object of type

Rectangle associated with the class RectangleRectangle associated with the class Rectangle Length and width Length and width Perimeter and area are derived from length and widthPerimeter and area are derived from length and width The data members of the class Rectangle are length and The data members of the class Rectangle are length and

widthwidth Identify methods (operations) needed for the class Identify methods (operations) needed for the class

RectangleRectangle Extract the verbs from the problem statementExtract the verbs from the problem statement Input, calculate, printInput, calculate, print List the required operations List the required operations

Page 14: Chapter 6

Object-Oriented Design Example: 2Object-Oriented Design Example: 2 Actions: Input, calculate, printActions: Input, calculate, print List the required operations List the required operations

Input: setLength, setWidthInput: setLength, setWidth Calculate: computePerimeter, computeAreaCalculate: computePerimeter, computeArea Print: printArea, printPerimeterPrint: printArea, printPerimeter Access: getLength, getWidthAccess: getLength, getWidth

Page 15: Chapter 6

class Rectangle with Data class Rectangle with Data Members and OperationsMembers and Operations

Page 16: Chapter 6

Object-Oriented Design: Example 1Object-Oriented Design: Example 1

Candy may be bought from a candy machine. A new Candy may be bought from a candy machine. A new candy machine is bought for the gym, but it is not candy machine is bought for the gym, but it is not working properly. The candy machine has four working properly. The candy machine has four dispensers to hold and release items sold by the candy dispensers to hold and release items sold by the candy machine and a cash register. The machine sells four machine and a cash register. The machine sells four products —candies, chips, gum, and cookies—each products —candies, chips, gum, and cookies—each stored in a separate dispenser. You have been asked stored in a separate dispenser. You have been asked to write a program for this candy machine so that it to write a program for this candy machine so that it can be put into operation.can be put into operation.

Page 17: Chapter 6

Object-Oriented Design Example: 2Object-Oriented Design Example: 2

The program should do the following:The program should do the following: Show Show the the customer customer the different the different products products sold by the sold by the

candy machinecandy machine.. Let the Let the customer customer make make the selection.the selection. Show Show the the customer customer the the cost of the item cost of the item selected.selected. Accept Accept money money from the from the customercustomer.. Return Return changechange.. Release Release the the itemitem, that is, , that is, make make the sale.the sale.

Page 18: Chapter 6

Object-Oriented Design Example: 3Object-Oriented Design Example: 3

CandyCandy may be bought from a may be bought from a candy machinecandy machine. A new . A new candy machinecandy machine is bought for the is bought for the gymgym, but it is not , but it is not working properly. The working properly. The candy machinecandy machine has four has four dispensersdispensers to hold and release to hold and release itemsitems sold by the sold by the candy candy machinemachine and a and a cash registercash register. The . The candy machinecandy machine sells sells four four productsproducts — —candies, chips, gumcandies, chips, gum, and , and cookiescookies——each stored in a separate each stored in a separate dispenserdispenser. You have been . You have been asked to write a program for this candy machine so asked to write a program for this candy machine so that it can be put into operation.that it can be put into operation.

Page 19: Chapter 6

Object-Oriented Design Example: 4Object-Oriented Design Example: 4

The program should do the following:The program should do the following: Show the Show the customercustomer the different the different products products sold by the sold by the

candy machinecandy machine.. Let the Let the customercustomer make the selection. make the selection. Show the Show the customercustomer the the cost of the itemcost of the item selected. selected. Accept Accept moneymoney from the from the customer.customer. Return Return changechange.. Release the Release the itemitem, that is, make the sale., that is, make the sale.

Page 20: Chapter 6

Object-Oriented Design Object-Oriented Design Example 2Example 2

Page 21: Chapter 6

Identify Members of Each classIdentify Members of Each class

Dispenser: must contain at least one item to Dispenser: must contain at least one item to and that items cost to make the saleand that items cost to make the sale Number of itemsNumber of items Cost of itemCost of item

Cash Register:Cash Register: Cash on handCash on hand

Candy Machine: Candy Machine: 4 dispensers: name for item being held4 dispensers: name for item being held 1 cash register1 cash register

Page 22: Chapter 6

Object-Oriented Design: Example 5Object-Oriented Design: Example 5

Candy may be Candy may be boughtbought from a candy machine. A new from a candy machine. A new candy machine is bought for the gym, but it is not candy machine is bought for the gym, but it is not working properly. The candy machine has four working properly. The candy machine has four dispensers to dispensers to holdhold and and releaserelease items items soldsold by the candy by the candy machine and a cash register. The machine machine and a cash register. The machine sellssells four four products —candies, chips, gum, and cookies—each products —candies, chips, gum, and cookies—each storedstored in a separate dispenser. You have been asked in a separate dispenser. You have been asked to write a program for this candy machine so that it to write a program for this candy machine so that it can be put into operation.can be put into operation.

Page 23: Chapter 6

Object-Oriented Design Example: 6Object-Oriented Design Example: 6

The program should do the following:The program should do the following: ShowShow the customer the different products the customer the different products soldsold by the by the

candy machine.candy machine. Let the customer Let the customer make the selectionmake the selection.. ShowShow the customer the cost of the item selected. the customer the cost of the item selected. AcceptAccept money from the customer. money from the customer. ReturnReturn change. change. ReleaseRelease the item, that is, the item, that is, make the salemake the sale..

Page 24: Chapter 6

Object-Oriented Design Object-Oriented Design Example 2Example 2

Page 25: Chapter 6

Implementing Classes and Implementing Classes and OperationsOperations

Algorithms are used to implement operationsAlgorithms are used to implement operations Construct and implement your own methodsConstruct and implement your own methods classes Integer, Double, Character, Long, Float classes Integer, Double, Character, Long, Float

Known as wrapper classesKnown as wrapper classes Provided so that values of primitive data types can be Provided so that values of primitive data types can be

treated as objectstreated as objects Have limitations (cannot change value stored in Have limitations (cannot change value stored in

objects)objects)

Page 26: Chapter 6

The class IntClassThe class IntClass

Page 27: Chapter 6

The class IntClassThe class IntClass

Page 28: Chapter 6

Chapter SummaryChapter Summary

Every GUI contains a windowEvery GUI contains a window Various components are added to content pane of windowVarious components are added to content pane of window class Frame is used to create windowsclass Frame is used to create windows Label is used to label GUI components and display Label is used to label GUI components and display

information to userinformation to user Textile is used for input/outputTextile is used for input/output JButton generates action eventJButton generates action event Action event is sent to action listenerAction event is sent to action listener Action listener must have method called actionPerformedAction listener must have method called actionPerformed

Page 29: Chapter 6

Chapter SummaryChapter Summary

class: collection of data members and methods class: collection of data members and methods associated with those membersassociated with those members

Object-Oriented Design (OOD)Object-Oriented Design (OOD) Starts with a problem statementStarts with a problem statement Identifies classes required with nouns in problem Identifies classes required with nouns in problem

statementstatement Identifies methods required with verbs in problem Identifies methods required with verbs in problem

specificationspecification