Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)"— Presentation transcript:

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

2 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  To create button  Declare reference variable of type JButton  Instantiate object  To add a text field to a container (display the label in the content pane) use the method add from the container class

3 Methods Provided by the class JButton

4

5 Handling an Event  Action event: event created when JButton is clicked  Event listener: object which receives message when JButton is clicked  In Java, you must register the listener  Specify the listener object for a particular button

6 Handling an Event  class ActionListener  Handles action event  Part of package java.awt.Event  The class ActionListener is a special type of class (interface)  Must contain actionPerformed method

7 Rectangle Program: Sample Run

8 Programming Example: Temperature Conversion  Input: temperature in Fahrenheit or Centigrade  Output: temperature in Centigrade if input is Fahrenheit; temperature in Fahrenheit if input is Centigrade

9 Programming Example: Temperature Conversion  Solution:  Create the appropriate JLabels, JTextFields, JButtons  Add them to the created content pane  Calculate the appropriate conversions when the buttons are clicked and an event is triggered

10 Sample Run for TempConversion

11 Object-Oriented Design  Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns and verbs 3. From list of nouns, select objects 4. Identify data components of each object 5. From list of verbs, select operations

12 Object-Oriented Design Example: 1  Problem Statement  Write a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle  Select Nouns  Length, width, rectangle, perimeter, area  Identify Classes  Length, width, perimeter and area are properties of a rectangle:  Rectangle will be the class

13 Object-Oriented Design Example: 2  Select essential attributes for the object of type Rectangle associated with the class Rectangle  Length and width  Perimeter and area are derived from length and width  The data members of the class Rectangle are length and width  Identify methods (operations) needed for the class Rectangle  Extract the verbs from the problem statement  Input, calculate, print  List the required operations

14 Object-Oriented Design Example: 2  Actions: Input, calculate, print  List the required operations  Input: setLength, setWidth  Calculate: computePerimeter, computeArea  Print: printArea, printPerimeter  Access: getLength, getWidth

15 class Rectangle with Data Members and Operations

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

17 Object-Oriented Design Example: 2 The program should do the following:  Show the customer the different products sold by the candy machine.  Let the customer make the selection.  Show the customer the cost of the item selected.  Accept money from the customer.  Return change.  Release the item, that is, make the sale.

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

19 Object-Oriented Design Example: 4 The program should do the following:  Show the customer the different products sold by the candy machine.  Let the customer make the selection.  Show the customer the cost of the item selected.  Accept money from the customer.  Return change.  Release the item, that is, make the sale.

20 Object-Oriented Design Example 2

21 Identify Members of Each class  Dispenser: must contain at least one item to and that items cost to make the sale  Number of items  Cost of item  Cash Register:  Cash on hand  Candy Machine:  4 dispensers: name for item being held  1 cash register

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

23 Object-Oriented Design Example: 6 The program should do the following:  Show the customer the different products sold by the candy machine.  Let the customer make the selection.  Show the customer the cost of the item selected.  Accept money from the customer.  Return change.  Release the item, that is, make the sale.

24 Object-Oriented Design Example 2

25 Implementing Classes and Operations  Algorithms are used to implement operations  Construct and implement your own methods  classes Integer, Double, Character, Long, Float  Known as wrapper classes  Provided so that values of primitive data types can be treated as objects  Have limitations (cannot change value stored in objects)

26 The class IntClass

27

28 Chapter Summary  Every GUI contains a window  Various components are added to content pane of window  class Frame is used to create windows  Label is used to label GUI components and display information to user  Textile is used for input/output  JButton generates action event  Action event is sent to action listener  Action listener must have method called actionPerformed

29 Chapter Summary  class: collection of data members and methods associated with those members  Object-Oriented Design (OOD)  Starts with a problem statement  Identifies classes required with nouns in problem statement  Identifies methods required with verbs in problem specification


Download ppt "Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)"

Similar presentations


Ads by Google