Lecture 5 Objectives s Learn about basic GUI

Lecture 5 Objectives s Learn about basic GUI components. s Explore how the GUI components JFrame, JLabel, JText. Field, and JButton work. s Become familiar with the concept of eventdriven programming. Java Programming: From Problem Analysis to Program Design, Second Edition

Lecture 5 Objectives s Discover events and event handlers. s Explore object-oriented design. s Learn how to identify objects, classes, and members of a class. Java Programming: From Problem Analysis to Program Design, Second Edition

Graphical User Interface (GUI) Components s s View inputs and outputs simultaneously. One graphical window. Input values in any order. Change input values in window. Click buttons to get output. Java Programming: From Problem Analysis to Program Design, Second Edition

Java GUI Components Java Programming: From Problem Analysis to Program Design, Second Edition 4

Graphical User Interface (GUI) Components s GUI components placed in content pane. s GUI components: s s Windows Labels Text areas Buttons Java Programming: From Problem Analysis to Program Design, Second Edition

GUI Components s Added to content pane of window. s Not added to window itself. s Pixel: A picture element. Java Programming: From Problem Analysis to Program Design, Second Edition

Windows s Can be created using a Frame object. s The class Frame provides various methods to control attributes of a window. s Measured in pixels of height and width. s Attributes associated with windows: s Title s Width s Height Java Programming: From Problem Analysis to Program Design, Second Edition

class JFrame s GUI window instance created as instance of Frame. s Provides various methods to control window attributes. Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JFrame Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JFrame Java Programming: From Problem Analysis to Program Design, Second Edition

Two Ways to Create a Window s First way: s Declare object of type JFrame. s Instantiate object. s Use various methods to manipulate window. s Second way: s Create class-containing application program by extending definition of class JFrame. s Utilize mechanism of inheritance. Java Programming: From Problem Analysis to Program Design, Second Edition

Content Pane s Inner area of GUI window (below title bar, inside border). s To access content pane: s Declare reference variable of type Container. s Use method get. Content. Pane of class JFrame. Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class Container Java Programming: From Problem Analysis to Program Design, Second Edition

class JLabel s Labels: Objects of a particular class type. s class JLabel: Used to create labels. s Label attributes: s Title s Width s Height s To create a label: s Instantiate object of type JLabel. s Modify attributes to control display of labels. Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JLabel Java Programming: From Problem Analysis to Program Design, Second Edition 15

Methods Provided by the class JLabel Java Programming: From Problem Analysis to Program Design, Second Edition 16

class JText. Field s Text fields: Objects belonging to class JText. Field. s To create a text field: s Declare reference variable of type JText. Field. s Instantiate object. Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JText. Field Java Programming: From Problem Analysis to Program Design, Second Edition

class JButton s Provided to create buttons in Java. s To create a button: s Use the same technique that is used to create JLabel and JText. Field. Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JButton Java Programming: From Problem Analysis to Program Design, Second Edition

Methods Provided by the class JButton Java Programming: From Problem Analysis to Program Design, Second Edition

Handling an Event s Action event: An event created when JButton is clicked. s Event listener: An object that receives a message when JButton is clicked. s In Java, you must register the listener. Java Programming: From Problem Analysis to Program Design, Second Edition

Handling an Event s class Action. Listener s Handles action event. s Part of package java. awt. Event. s The class Action. Listener is a special type of class (interface). s Must contain the action. Performed method. Java Programming: From Problem Analysis to Program Design, Second Edition

Rectangle Program: Sample Run Java Programming: From Problem Analysis to Program Design, Second Edition 24

Programming Example: Temperature Conversion s Input: Temperature in Fahrenheit or Celsius. s Output: Temperature in Celsius if input is Fahrenheit; temperature in Fahrenheit if input is Celsius. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Temperature Conversion Solution: 1. Create the appropriate JLabels, JText. Fields, JButtons. 2. Add them to the created content pane. 3. Calculate the appropriate conversions when the buttons are clicked an event is triggered. Java Programming: From Problem Analysis to Program Design, Second Edition

Sample Run for Temp. Conversion Java Programming: From Problem Analysis to Program Design, Second Edition 27

Object-Oriented Design Simplified methodology: 1. 2. 3. 4. 5. Write down detailed description of problem. Identify all (relevant) nouns and verbs. From list of nouns, select objects. Identify data components of each object. From list of verbs, select operations. Java Programming: From Problem Analysis to Program Design, Second Edition

Object-Oriented Design: Example 1 s Problem statement: s Write a program to input the length and width of a rectangle, and calculate and print the perimeter and area of the rectangle. s Nouns: s Length, width, rectangle, perimeter, area. Java Programming: From Problem Analysis to Program Design, Second Edition

class Rectangle with Data Members and Operations Java Programming: From Problem Analysis to Program Design, Second Edition 30

Object-Oriented Design: Example 2 A place to buy candy is 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 of which is 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. Java Programming: From Problem Analysis to Program Design, Second Edition

Object-Oriented Design: Example 2 The program should do the following: s Show the customer the different products sold by the candy machine. s Let the customer make the selection. s Show the customer the cost of the item selected. s Accept money from the customer. s Return change. s Release the item, that is, make the sale. Java Programming: From Problem Analysis to Program Design, Second Edition

Object-Oriented Design: Example 2 Java Programming: From Problem Analysis to Program Design, Second Edition

Object-Oriented Design: Example 2 Java Programming: From Problem Analysis to Program Design, Second Edition

Implementing Classes and Operations s Algorithms are used to implement operations. s Construct and implement your own methods. s Classes Integer, Double, Character, Long, Float: s Known as wrapper classes. s Provided so that values of primitive data types can be treated as objects. s Have limitations (cannot change value stored in objects). Java Programming: From Problem Analysis to Program Design, Second Edition 35

The class Integer Java Programming: From Problem Analysis to Program Design, Second Edition

The class Integer Java Programming: From Problem Analysis to Program Design, Second Edition

The class Integer num; num = new Integer(86) Java Programming: From Problem Analysis to Program Design, Second Edition

The class Integer int x; Integer num; num = 25; This statement is equivalent to the statement: num = new Integer(25); The expression: num = 25; is referred to as autoboxing of the int type. Java Programming: From Problem Analysis to Program Design, Second Edition

The class Integer int x; Integer num; The statement: x = num; This statement is equivalent to the statement: x = num. int. Value(); This statement is referred to as auto unboxing of the int type. Java Programming: From Problem Analysis to Program Design, Second Edition

The class Integer s To compare the values of two Integer objects, you can use the method compare. To. s If you want to compare the values of two Integer objects only for equality, then you can use the method equal. Java Programming: From Problem Analysis to Program Design, Second Edition

The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition 42

The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition

Lecture 5 Summary s Every GUI contains a window. s Various components are added to the content pane of a window. s class Frame is used to create windows. s JLabel is used to label GUI components and display information to user. s JText. Filed is used for input/output. s JButton generates action event. s Action event is sent to action listener. s Action listener must have method called action. Performed. Java Programming: From Problem Analysis to Program Design, Second Edition

Lecture 5 Summary s A class is a collection of data members and methods associated with those members. s Object-oriented design (OOD): s Starts with a problem statement. s Identifies required classes with nouns in problem statement. s Identifies required methods with verbs in problem statement. Java Programming: From Problem Analysis to Program Design, Second Edition
- Slides: 45