0
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented...
Chapter Objectives <ul><li>Learn about basic GUI components </li></ul><ul><li>Explore how the GUI components  JFrame ,  JL...
Chapter Objectives <ul><li>Discover events and event handlers </li></ul><ul><li>Explore object-oriented design </li></ul><...
Graphical User Interface (GUI) Components <ul><li>View inputs and outputs simultaneously  </li></ul><ul><li>One graphical ...
Java GUI Components Java Programming: From Problem Analysis to Program Design, 3e
Graphical User Interface (GUI) Components (continued) <ul><li>GUI components placed in content pane </li></ul><ul><li>GUI ...
GUI Components <ul><li>Added to content pane of window </li></ul><ul><li>Not added to window itself </li></ul><ul><li>Pixe...
Windows <ul><li>Can be created using a  JFrame  object </li></ul><ul><li>The class  JFrame  provides various methods to co...
class  JFrame <ul><li>GUI window instance created as instance of  JFrame </li></ul><ul><li>Provides various methods to con...
Methods Provided by the  class   JFrame Java Programming: From Problem Analysis to Program Design, 3e
Methods Provided by the  class   Jframe  (continued) Java Programming: From Problem Analysis to Program Design, 3e
Two Ways to Create a Window <ul><li>First way  </li></ul><ul><ul><li>Declare object of type  JFrame </li></ul></ul><ul><ul...
Content Pane <ul><li>Inner area of GUI window (below title bar, inside border) </li></ul><ul><li>To access content pane </...
Methods Provided by the class Container Java Programming: From Problem Analysis to Program Design, 3e
class  JLabel <ul><li>Labels: objects of particular class type </li></ul><ul><li>class   JLabel : used to create labels </...
class  Jlabel  (continued) Java Programming: From Problem Analysis to Program Design, 3e
class   JTextField <ul><li>Text fields: objects belonging to  class   JTextField </li></ul><ul><li>To create text field </...
class   JTextField  (continued) Java Programming: From Problem Analysis to Program Design, 3e
class   JTextField  (continued) Java Programming: From Problem Analysis to Program Design, 3e
class   JButton <ul><li>Provided to create buttons in Java </li></ul><ul><li>To create button </li></ul><ul><ul><li>Same t...
class   Jbutton  (continued) Java Programming: From Problem Analysis to Program Design, 3e
Handling an Event <ul><li>Action event: event created when  JButton  is clicked </li></ul><ul><li>Event listener: object t...
Handling an Event (continued) <ul><li>class  ActionListener </li></ul><ul><ul><li>Handles action event  </li></ul></ul><ul...
Rectangle Program: Sample Run Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Temperature Conversion <ul><li>Input:  temperature in Fahrenheit or  Celsius  </li></ul><ul><li>Outpu...
Programming Example: Temperature Conversion (continued) <ul><li>Solution </li></ul><ul><ul><li>Create the appropriate  JLa...
Sample Run for  TempConversion Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design <ul><li>Simplified methodology </li></ul><ul><ul><li>1. Write down detailed description of problem ...
Object-Oriented Design  Example 1 <ul><li>Problem Statement </li></ul><ul><ul><li>Write a program to input the length and ...
class  Rectangle  with Data Members and Operations Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design  Example 2 <ul><li>An inoperable candy machine has a cash register and four dispensers to hold and ...
Object-Oriented Design  Example 2 (continued) <ul><li>The program should do the following: </li></ul><ul><ul><li>Show  the...
Object-Oriented Design  Example 2 (continued) Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design  Example 2 (continued) Java Programming: From Problem Analysis to Program Design, 3e
Implementing Classes and Operations <ul><li>Algorithms are used to implement operations </li></ul><ul><li>Construct and im...
The  class  Integer Java Programming: From Problem Analysis to Program Design, 3e
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e Integer num;  num =  new  I...
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e int  x;  Integer num;  num ...
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e int  x;  Integer num;   The...
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e <ul><li>To compare the valu...
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e
The  class  Integer  (continued) Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary <ul><li>Every GUI contains a window </li></ul><ul><li>Various components are added to content pane of wind...
Chapter Summary (continued) <ul><li>Action listener must have method called  actionPerformed </li></ul><ul><li>class: coll...
Upcoming SlideShare
Loading in...5
×

Chap06

519

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
519
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Chap06"

  1. 1. Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
  2. 2. Chapter Objectives <ul><li>Learn about basic GUI components </li></ul><ul><li>Explore how the GUI components JFrame , JLabel , JTextField , and JButton work </li></ul><ul><li>Become familiar with the concept of event-driven programming </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  3. 3. Chapter Objectives <ul><li>Discover events and event handlers </li></ul><ul><li>Explore object-oriented design </li></ul><ul><li>Learn how to identify objects, classes, and members of a class </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  4. 4. Graphical User Interface (GUI) Components <ul><li>View inputs and outputs simultaneously </li></ul><ul><li>One graphical window </li></ul><ul><li>Input values in any order </li></ul><ul><li>Change input values in window </li></ul><ul><li>Click on buttons to get output </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  5. 5. Java GUI Components Java Programming: From Problem Analysis to Program Design, 3e
  6. 6. Graphical User Interface (GUI) Components (continued) <ul><li>GUI components placed in content pane </li></ul><ul><li>GUI components </li></ul><ul><ul><li>Windows </li></ul></ul><ul><ul><li>Labels </li></ul></ul><ul><ul><li>Text areas </li></ul></ul><ul><ul><li>Buttons </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  7. 7. GUI Components <ul><li>Added to content pane of window </li></ul><ul><li>Not added to window itself </li></ul><ul><li>Pixel: picture element </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  8. 8. Windows <ul><li>Can be created using a JFrame object </li></ul><ul><li>The class JFrame provides various methods to control attributes of a window </li></ul><ul><li>Measured in pixels of height and width </li></ul><ul><li>Attributes associated with windows </li></ul><ul><ul><li>Title </li></ul></ul><ul><ul><li>Width </li></ul></ul><ul><ul><li>Height </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  9. 9. class JFrame <ul><li>GUI window instance created as instance of JFrame </li></ul><ul><li>Provides various methods to control window attributes </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  10. 10. Methods Provided by the class JFrame Java Programming: From Problem Analysis to Program Design, 3e
  11. 11. Methods Provided by the class Jframe (continued) Java Programming: From Problem Analysis to Program Design, 3e
  12. 12. Two Ways to Create a Window <ul><li>First way </li></ul><ul><ul><li>Declare object of type JFrame </li></ul></ul><ul><ul><li>Instantiate object </li></ul></ul><ul><ul><li>Use various methods to manipulate window </li></ul></ul><ul><li>Second way </li></ul><ul><ul><li>Create class containing application program by extending definition of class JFrame </li></ul></ul><ul><ul><li>Utilizes mechanism of inheritance </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  13. 13. Content Pane <ul><li>Inner area of GUI window (below title bar, inside border) </li></ul><ul><li>To access content pane </li></ul><ul><ul><li>Declare reference variable of type Container </li></ul></ul><ul><ul><li>Use method getContentPane of class JFrame </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  14. 14. Methods Provided by the class Container Java Programming: From Problem Analysis to Program Design, 3e
  15. 15. class JLabel <ul><li>Labels: objects of particular class type </li></ul><ul><li>class JLabel : used to create labels </li></ul><ul><li>Label attributes </li></ul><ul><ul><li>Title </li></ul></ul><ul><ul><li>Width </li></ul></ul><ul><ul><li>Height </li></ul></ul><ul><li>To create a label </li></ul><ul><ul><li>Instantiate object of type JLabel </li></ul></ul><ul><ul><li>Modify attributes to control display of labels </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  16. 16. class Jlabel (continued) Java Programming: From Problem Analysis to Program Design, 3e
  17. 17. class JTextField <ul><li>Text fields: objects belonging to class JTextField </li></ul><ul><li>To create text field </li></ul><ul><ul><li>Declare reference variable of type JTextField </li></ul></ul><ul><ul><li>Instantiate object </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  18. 18. class JTextField (continued) Java Programming: From Problem Analysis to Program Design, 3e
  19. 19. class JTextField (continued) Java Programming: From Problem Analysis to Program Design, 3e
  20. 20. class JButton <ul><li>Provided to create buttons in Java </li></ul><ul><li>To create button </li></ul><ul><ul><li>Same technique as creating JLabel and JTextField </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  21. 21. class Jbutton (continued) Java Programming: From Problem Analysis to Program Design, 3e
  22. 22. Handling an Event <ul><li>Action event: event created when JButton is clicked </li></ul><ul><li>Event listener: object that receives message when JButton is clicked </li></ul><ul><li>In Java, you must register the listener </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  23. 23. Handling an Event (continued) <ul><li>class ActionListener </li></ul><ul><ul><li>Handles action event </li></ul></ul><ul><ul><li>Part of package java.awt.Event </li></ul></ul><ul><ul><li>The class ActionListener is a special type of class (interface) </li></ul></ul><ul><ul><li>Must contain actionPerformed method </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  24. 24. Rectangle Program: Sample Run Java Programming: From Problem Analysis to Program Design, 3e
  25. 25. Programming Example: Temperature Conversion <ul><li>Input: temperature in Fahrenheit or Celsius </li></ul><ul><li>Output: temperature in Celsius if input is Fahrenheit; temperature in Fahrenheit if input is Celsius </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  26. 26. Programming Example: Temperature Conversion (continued) <ul><li>Solution </li></ul><ul><ul><li>Create the appropriate JLabels , JTextFields , JButtons </li></ul></ul><ul><ul><li>Add them to the created content pane </li></ul></ul><ul><ul><li>Calculate the appropriate conversions when the buttons are clicked and an event is triggered </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  27. 27. Sample Run for TempConversion Java Programming: From Problem Analysis to Program Design, 3e
  28. 28. Object-Oriented Design <ul><li>Simplified methodology </li></ul><ul><ul><li>1. Write down detailed description of problem </li></ul></ul><ul><ul><li>2. Identify all (relevant) nouns and verbs </li></ul></ul><ul><ul><li>3. From list of nouns, select objects </li></ul></ul><ul><ul><li>4. Identify data components of each object </li></ul></ul><ul><ul><li>5. From list of verbs, select operations </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  29. 29. Object-Oriented Design Example 1 <ul><li>Problem Statement </li></ul><ul><ul><li>Write a program to input the length and width of a rectangle, and calculate and print the perimeter and area of the rectangle </li></ul></ul><ul><li>Nouns </li></ul><ul><ul><li>Length, width, rectangle, perimeter, area </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  30. 30. class Rectangle with Data Members and Operations Java Programming: From Problem Analysis to Program Design, 3e
  31. 31. Object-Oriented Design Example 2 <ul><li>An inoperable candy machine has a cash register and four dispensers to hold and release items sold by the machine </li></ul><ul><li>The machine sells: candies, chips, gum, and cookies </li></ul><ul><li>Write a program for this candy machine so that it can be put into operation </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  32. 32. Object-Oriented Design Example 2 (continued) <ul><li>The program should do the following: </li></ul><ul><ul><li>Show the customer the different products sold by the candy machine </li></ul></ul><ul><ul><li>Let the customer make the selection </li></ul></ul><ul><ul><li>Show the customer the cost of the item selected </li></ul></ul><ul><ul><li>Accept money from the customer </li></ul></ul><ul><ul><li>Return change </li></ul></ul><ul><ul><li>Release the item ; that is, make the sale </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  33. 33. Object-Oriented Design Example 2 (continued) Java Programming: From Problem Analysis to Program Design, 3e
  34. 34. Object-Oriented Design Example 2 (continued) Java Programming: From Problem Analysis to Program Design, 3e
  35. 35. Implementing Classes and Operations <ul><li>Algorithms are used to implement operations </li></ul><ul><li>Construct and implement your own methods </li></ul><ul><li>classes Integer , Double , Character , Long , Float </li></ul><ul><ul><li>Known as wrapper classes </li></ul></ul><ul><ul><li>Provided so that values of primitive data types can be treated as objects </li></ul></ul><ul><ul><li>Have limitations (cannot change value stored in objects) </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  36. 36. The class Integer Java Programming: From Problem Analysis to Program Design, 3e
  37. 37. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e
  38. 38. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e
  39. 39. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e Integer num; num = new Integer(86)
  40. 40. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e int x; Integer num; num = 25; For the most part, this statement is similar to the statement: num = new Integer(25); The expression: num = 25; is referred to as autoboxing of the int type
  41. 41. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e int x; Integer num; The statement: x = num; This statement is equivalent to the statement: x = num.intValue(); This statement is referred to as auto unboxing of the int type
  42. 42. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e <ul><li>To compare the values of two Integer objects, you can use the method compareTo </li></ul><ul><li>If you want to compare the values of two Integer objects only for equality, then you can use the method equals </li></ul>
  43. 43. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e
  44. 44. The class Integer (continued) Java Programming: From Problem Analysis to Program Design, 3e
  45. 45. Chapter Summary <ul><li>Every GUI contains a window </li></ul><ul><li>Various components are added to content pane of window </li></ul><ul><li>class JFrame is used to create windows </li></ul><ul><li>JLabel is used to label GUI components and display information to user </li></ul><ul><li>JTextFiled is used for input/output </li></ul><ul><li>JButton generates action event </li></ul><ul><li>Action event is sent to action listener </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  46. 46. Chapter Summary (continued) <ul><li>Action listener must have method called actionPerformed </li></ul><ul><li>class: collection of data members and methods associated with those members </li></ul><ul><li>Object-Oriented Design (OOD) </li></ul><ul><ul><li>Starts with a problem statement </li></ul></ul><ul><ul><li>Identifies classes required with nouns in problem statement </li></ul></ul><ul><ul><li>Identifies methods required with verbs in problem specification </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×