Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Slides:



Advertisements
Similar presentations
Interfaces A Java interface is a collection
Advertisements

Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 5: Enhancing Classes
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Abstract Classes.
Chapter Day 14. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 14 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 11.
Chapter 4 Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Object Oriented Programming.  Interface  Event Handling.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
A First Look at GUI Applications
Processing Timer Events
Chapter 5: Enhancing Classes
Java Programming: From Problem Analysis to Program Design,
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
CSI 1102 Introduction to Software Design
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Object-Oriented Design Part 2
Presentation transcript:

Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used with permission. All rights reserved.

Keyboard Input Revisited  The Keyboard class was introduced in Chapter 2 to facilitate capturing input from the keyboard  Recall that the Keyboard class was written by the authors of the book  The Keyboard class hides various aspects of Java input processing  But it is important to understand how that processing works

Reading Keyboard Input  Java I/O is accomplished using objects that represent streams of data  A stream is an ordered sequence of bytes  The System.out object represents a standard output stream, which defaults to the monitor screen  Reading keyboard input is more complicated  The input stream is made up of multiple objects: BufferedReader in = new BufferedReader ( new InputStreamReader (System.in));

Reading Keyboard Input  The System.in object is used to create an InputStreamReader object  The InputStreamReader object is used to create a BufferedReader object  This creates an input stream that treats input as characters and buffers them so that input can be read a line at a time  The readLine method of the BufferedReader class reads an entire line of input as a String  Wrapper class methods can be used to convert numeric input

Reading Keyboard Input  Problems that arise in reading or converting a value manifest themselves as exceptions  The throws clause of a method header indicates what exceptions it may throw  See Wages2.java (page 289)Wages2.java  I/O and exceptions are explored further in Chapter 8

Nested Classes  In addition to containing data and methods, a class can contain other classes  A class declared within another class is called a nested class Enclosing Class Nested Class

Nested Classes  A nested class has access to the variables and methods of the enclosing class, even if they are declared private  In certain situations this makes the implementation of the classes easier because they can share information easily  Furthermore, the nested class can be protected by the enclosing class from external use  This is a special relationship and should be used with care

Nested Classes  A nested class produces a separate bytecode file  If a nested class called Inside is declared in an outer class called Outside, two bytecode files are produced: Outside.class Outside$Inside.class  Nested classes can be declared as static, in which case they cannot refer to instance variables or methods

Inner Classes  A nonstatic nested class is called an inner class  An inner class instance is associated with each instance of the enclosing class  An instance of an inner class can exist only within an instance of an enclosing class  See TestInner.java (page 291)TestInner.java  See Outer.java (page 292)Outer.java

Interfaces  A Java interface is a collection of abstract methods and constants  An abstract method is a method header without a method body  An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off  An interface is used to establish, as a formal contract, a set of methods that a class will implement

Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

Interfaces  An interface cannot be instantiated  Methods in an interface have public visibility by default  A class formally implements an interface by stating so in the class header providing implementations for each abstract method in the interface  If a class asserts that it implements an interface, it must define all methods in the interface

Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

Interfaces  A class that implements an interface can implement other methods as well  See Complexity.java (page 294)Complexity.java  See Question.java (page 295)Question.java  See MiniQuiz.java (page 297)MiniQuiz.java  In addition to (or instead of) abstract methods, an interface can contain constants  When a class implements an interface, it gains access to all its constants

Interfaces  A class can implement multiple interfaces  The interfaces are listed in the implements clause  The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

Interfaces  The Java standard class library contains many helpful interfaces  The Comparable interface contains an abstract method called compareTo, which is used to compare two objects  The String class implements Comparable, giving us the ability to put strings in lexicographic order  The Iterator interface contains methods that allow the user to move easily through a collection of objects

The Comparable Interface  The Comparable interface provides a common mechanism for comparing one object to another if (obj1.compareTo(obj2) < 0) System.out.println (“obj1 is less than obj2”);  The result is negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2  When a programmer writes a class that implements the Comparable interface, it should follow this intent  It's up to the programmer to determine what makes one object “less” than another

The Iterator Interface  The Iterator interface provides a means of moving through a collection of objects, one at a time  The hasNext method returns a boolean result (true if there are items left to process)  The next method returns the next object in the iteration  The remove method removes the object most recently returned by the next method

Dialog Boxes  A dialog box is a graphical window that pops up on top of any currently active window for the user  The Swing API contains a class called JOptionPane that simplifies the creation and use of basic dialog boxes  There are three categories of JOptionPane dialog boxes A message dialog displays an output string An input dialog presents a prompt and a single input text field A confirm dialog presents the user with a simple yes-or-no question  See EvenOdd.java (page 302)EvenOdd.java

The EvenOdd Program

Graphical User Interfaces  A Graphical User Interface (GUI) is created with at least three kinds of objects components events listeners  A GUI component defines a screen element to display information or allow the user to interact with the program buttons, text fields, labels, menus, etc.  A container is a special component that holds and organizes other components dialog boxes, applets, frames, panels, etc.

Events  An event is an object that represents some activity to which we may want to respond  For example, we may want our program to perform some action when the following occurs: the mouse is moved a mouse button is clicked the mouse is dragged a graphical button is clicked a keyboard key is pressed a timer expires  Events often correspond to user actions, but not always

Events and Listeners  The Java standard class library contains several classes that represent typical events  Components, such as an applet or a graphical button, generate (fire) an event when it occurs  Other objects, called listeners, wait for events to occur  We can write listener objects to do whatever we want when an event occurs  A listener object is often defined using an inner class

Events and Listeners Component This object may generate an event Listener This object waits for and responds to an event Event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event

Listener Interfaces  We can create a listener object by writing a class that implements a particular listener interface  The Java standard class library contains several interfaces that correspond to particular event categories  For example, the MouseListener interface contains methods that correspond to mouse events  After creating the listener, we add the listener to the component that might generate the event to set up a formal relationship between the generator and listener

Creating GUIs  To create a program with a GUI: define and set up the components create listener objects set up the relationships between the listeners and the components that generate the events of interest define what happens in response to each event  A push button is a component that allows the user to initiate an action with the press of the mouse button defined by the JButton class generates an action event  A label is a component that displays a line of text (or an image, or both) defined by the JLabel class

Creating GUIs  The init method of an applet can be used to set up the GUI and add each component to the applet container  The Swing version of the Applet class is called JApplet  In a JApplet, components are added to the applet's content pane  The content pane is retrieved using the getContentPane method  A JButton generates an action event  See PushCounter.java (page 305)PushCounter.java

The PushCounter Program

Action Listeners  The interface corresponding to an action event is called ActionListener, which defines only one method, called actionPerformed  The ButtonListener inner class implements the ActionListener interface in the PushButton program  When the button is pushed, the JButton object invokes the actionPerformed method, passing it an ActionEvent  The listener method may or may not make use of the event object passed to it

GUI Applications  A frame is a container component used for stand-alone GUI-based applications  A panel is a container, but unlike a frame, it cannot be displayed on its own it must be added to another container it helps organize the components in a GUI  See Fahrenheit.java (page 309)Fahrenheit.java  See FahrenheitGUI.java (page 310)FahrenheitGUI.java

The Fahrenheit Program

Summary  Chapter 5 has focused on: object references and aliases passing objects references as parameters the static modifier wrapper classes nested classes and inner classes interfaces dialog boxes GUI components, events, and listeners