JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River,

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo Program l Layout Managers l Buttons and Action Listeners l Container.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
CS102--Object Oriented Programming Lecture 18: – The Swing Package Copyright © 2008 Xiaoyan Li.
Java Swing 4 th April 2008 CS 180 Department of Computer Science, Purdue University.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Programming Chapter 10 Graphical User Interfaces.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Topics Introduction Scene Graphs
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
PANELS. Color Class Colour constants are as follows: –Color.black –Color.blue import java.awt.* to access Color Class.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
Java Programming: From Problem Analysis to Program Design,
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc. All.
Comp 249 Programming Methodology
Window Interfaces Using Swing Objects Chapter 12
Presentation transcript:

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Window Interfaces Using Swing Chapter 13

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe the basics of event-driven programming Design and code a simple GUI that includes buttons and text Use the standard classes

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Graphical User Interfaces Referred to as GUI (pronounced "gooey") Elements of a GUI  Window – portion of the screen  Menu – list of choices  Button – click to choose an item

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Event Driven Programming An event is an object that represents an action  An object "fires" an event Figure 13.1 Event Firing And An Event Listener

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example Note example program, listing 13.1 class FirstSwingDemoexample program Window appears on screen with message Uses JFrame object Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example Note use of  JLabel object  Content pane, the inside of the window  Registering a listener  Setting the window to be visible

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example Figure 13.2 Clicking the Close-Window button View listener class, listing 13.2 class WindowDestroyerlistener class

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved More About Window Listeners Derived from class WindowAdapter Figure 13.3 Methods in the Class WindowAdapter

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved More About Window Listeners Figure 13.3 ctd.

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Size Units for Screen Objects Smallest screen area displayed is a pixel With Swing,  Both size and position of objects on screen measured in pixels. A screen’s resolution is a measure of the number of pixels it can display

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The setVisible Method Takes one argument of type boolean Other objects besides windows can be made visible or invisible  The object calls the method

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example A better version of first Swing program, listing 13.3 class FirstWindowbetter version A program that uses class FirstWindow listing 13.4 class FirstWindowDemoprogram Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Adding Items to a JFrame Window Requires the following syntax:

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example View new sample window, listing 13.5 class SecondWindownew sample Note new elements  A title, Second Window  A local variable named contentPane to reference the content pane of the window  A background color, blue  A new way to add the window listener

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved A Window with Color Note color constants Figure 13.4, the color constants View demo program, listing 13.6 class SecondWindowDemodemo program

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved A Window with Color Run of demo program Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Methods in Class JFrame Figure 13.5

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Methods in Class JFrame Figure 13.5 ctd.

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Layout Managers A layout manager arranges objects View example using BorderLayout listing 13.7 class BorderLayoutDemoexample Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Layout Managers Figure 13.6 Five regions of a BorderLayout manager Syntax

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Layout Managers Figure 13.7, some layout managers

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Button and Action Listeners Note sample program, listing 13.8 class ButtonDemosample program Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Buttons Adding buttons to a GUI  Create the instance of the button  Use the.add method Remember that the Close-Window button is not an object of the class JButton

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Action Listeners, Action Events Swing, GUI class definition should:  Register a listener for each button  Define a method to be invoked when the event is sent to the listener A button "fires" action events Action events are handled by action listeners

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Action Listeners, Action Events To make a class into an action-listener class you must:  Add the phrase implements ActionListener to the heading of the class definition.  Define a method named actionPerformed.

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Action Listeners, Action Events Figure 13.8 buttons and an action listener

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Model–View–Controller Pattern Figure 13.9 the model–view–controller pattern

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Container Classes Building a Swing GUI uses inheritance and containment Inheritance  Use the Swing class JFrame and make your window a derived class of JFrame Containment  Use one of the Swing classes as a container and to place elements in the container.

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Class JPanel A panel is a container Panels can have a color To add a button to a panel  Use add to place the button in the panel buttonPanel.add(stopButton);  Use add to place the panel in the frame’s content pane contentPane.add(buttonPanel, BorderLayout.SOUTH);

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Putting Buttons into a Panel View sample program, listing 13.9 class PanelDemosample program Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Class Container A container class is a descendant of the class Container A component class is a descendant of the class JComponent Figure hierarchy of swing classes...

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Class Container Figure hierarchy of swing classes, ctd....

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Three Kinds of Objects When using a Swing container class, you have three kinds of objects to deal with  The container class itself  The components you add to the container  A layout manager, which positions components inside container

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Text Areas, Text Fields A text area contains user input or program output View example program, listing class MemoSaverexample program Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example Labeling a text field Note sample program, listing class LabelDemosample program Sample screen output A label

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Numbers as Input, Output With Swing  All input from, output to the screen is of type string  If input is meant to be numeric, programmer must convert from string  Similarly numeric values for output must be converted to string

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming Example A GUI adding machine View program, listing class Adderprogram Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Catching NumberFormatException Problem with previous program  What happens if user enters non-numeric value in the text field? Program should catch that situation and throw an exception View improved program, listing class ImprovedAdderimproved program

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Catching NumberFormatException Program gives warning for invalid input Sample screen output

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary GUIs require event driven programming Use Swing with inheritance or add components to a container class Define windowing GUI as a derived class of the Swing class JFrame. Add string of text to GUI as a JLabel object

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary Click an object of type JButton to fire an event Event handled by actionlistener Use. add method to place elements in a container object For an object of the class JFrame use the method getContentPane

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary A panel is a container object that is used to group elements inside of a larger container. TextField and TextArea objects are used for text input, output