Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
Advertisements

ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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,
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Chapter Day 23. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 23 Problem set 4 Due Problem set 5 Posted (Last one)  Due Dec 8 Capstones.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
ITEC220 GUI Lecture – Part 1 References  Java Software Solutions,” by Lewis & Loftus  Chapter 3  Chapter 4  Chapter 5  Chapter 6  Java Foundations-Introduction.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Chapter 8: Writing Graphical User Interfaces
Introduction to Matlab & Data Analysis
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, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Chapter 5: Enhancing Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Graphical User Interfaces Revisited Material from Chapters
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
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.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Creating User Interfaces Event-Driven Programming.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Chapter 9: Graphical User Interfaces
A First Look at GUI Applications
Processing Timer Events
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Timer class and inner classes
Advanced GUIs and Graphics
ITEC220 GUI Lecture – Part 2 References
TA: Nouf Al-Harbi NoufNaief.net :::
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming and Data Structures by John Lewis, Peter DePasquale and Joseph Chase

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Graphical User Interfaces Many programs provide graphical user interfaces (GUI) through which a user interacts Chapter 6 focuses on –discussing the core elements: components, events, and listeners –discussing the concept of a layout manager –examining mouse and keyboard events –describing tool tips and mnemonics –discussing GUI design issues

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline GUI Elements More Components Layout Managers Mouse and Key Events Dialog Boxes Important Details GUI Design We’ll discuss these sections later We’ll discuss these sections later We’ll discuss these sections later

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.5 – Dialog Boxes A dialog box is a graphical window that pops up on top of any currently active window so that the user can interact with it A dialog box can serve a variety of purposes –conveying information –confirming an action –permitting the user to enter information The JOptionPane class simplifies the creation and use of basic dialog boxes

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.5 – Dialog Boxes JOptionPane dialog boxes fall into three categories –message dialog boxes – used to display an output string –input dialog boxes – presents a prompt and a single input txt file into which the user can enter one string of data –confirm dialog box – presents the user with a simple yes-or-no question These three basic types of dialog boxes are easily created using static methods in the JOptionPane class Many of the JOptionPane methods allow the program to tailor the contents of the dialog box

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.5 – Dialog Boxes See Listing 6.29

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline GUI Elements More Components Layout Managers Mouse and Key Events Dialog Boxes Important Details GUI Design

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Remember GUI Basics Frame Panel Text within a panel is a label. Dialog boxes pop up within the frame

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements Three kinds of objects are needed to create a GUI in Java –components –events –listeners Component – an object that defines a screen element used to display information or allow the user to interact with the program –A container is a special type of component that is used to hold and organize other components (e.g., frames, panels)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements Event – an object that represents some occurrence in which we may be interested –Often correspond to user actions (mouse button press, keyboard key press) –Most GUI components generate events to indicate a user action related to that component –Program that is oriented around GUI, responding to user events is called event-driven Events mean something has been triggered to happen! Very exciting!

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements Listener – an object that “waits” for an event to occur and responds in way when it does –In designing a GUI-based program we need to establish the relationships between the listener, the event it listens for, and the component that generates the event In this textbook, we use existing components and events from the Java class library. We will, however, write our own listener classes to perform whatever actions we desire when events occur

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements To create a Java program that uses a GUI, we must: 1.instantiate and set up the necessary components, 2.implement listener classes that define what happens when particular events occur, and 3.establish the relationship between the listeners and the components that generate the events of interest Java components and other GUI-related classes are defined primarily in two packages –java.awt –javax.swing

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements The Abstract Window Toolkit (AWT) was the original Java GUI package –contains many important classes we will use The Swing package was added later –provides components that are more versatile than those of AWT Let’s look at a simple example that contains all of the basic GUI elements –the example presents the user with a single push button –each time the button is pushed, a counter is updated and displayed

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – Push Counter Application See Listing 6.1 and 6.2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Push Counter – Activity Let’s modify the files so that there is a “Subtract” button that will decrement the count. Create new class “PushCounter2.java” and copy/paste “PushCounter.java” code. Make sure to update the class name. Create new class “PushCounterPanel2.java” and copy/paste “PushCounterPanel.java” code. Make sure to update the class and constructor names. In “PushCounter2.java” change reference to PushCounterPanel2. Make sure it still runs! In “PushCounterPanel2.java” –Add a new button variable named “pushSubtract” –Create the button objects with the text “Subtract!” –Add the new button, pushSubtract, to the panel –Write a new listener, ButtonListenerSubtract, that subtracts each time the push event occurs

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Generate JavaDoc for PushCounterPanel2 Notice the “Methods inherited” that are listed in the JavaDoc Where did they come from? –When we created the class “PushCounterPanel2” we used “extends” JPanel –“PushCounterPanel2” inherited all the features of JPanel. –Later this semester, we will focus on Object Oriented concepts and inheritance is an important characteristic.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1 – GUI Elements We classify containers as either –heavyweight – managed by the underlying operating system examples: a frame, an applet (used to display and execute a Java application through a Web browser) –lightweight – managed by the Java program example: a panel more complex than heavyweight containers Generally, we can create a GUI-based application by creating a frame in which the program interface is displayed

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline GUI Elements More Components Layout Managers Mouse and Key Events Dialog Boxes Important Details GUI Design

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – More Components In addition to push buttons, there are variety of other interactive components –text fields – allows the user to enter typed input from the keyboard –check boxes – a button that can be toggled on or off using the mouse (indicates a boolean value is set or unset) –radio buttons – used with other radio buttons to provide a set of mutually exclusive options –sliders – allows the user to specify a numeric value within a bounded range –combo boxes – allows the user to select one of several options from a “drop down” menu –timers – helps us manage an activity over time, has no visual representation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Fahrenheit Application See Listing 6.5 and 6.6

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Text Fields A text field generates an action event when the Enter or Return key is pressed (and the cursor is in the field) Note that the push button and the text field generate the same kind of event – an action event An alternative implementation could involve adding a push button to the panel which causes the conversion to occur when the user pushes the button

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Check Boxes A check box generates an item event when it changes state from selected (checked) to deselected (unchecked) and vice versa In the example, we use the same listener to handle both check boxes (bold and italic) The example also uses the Font class to display and change the text label Style of the font is represented as an integer, integer constants defined in the class are used to represent aspects of style The JCheckBox class is used to define check boxes

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Style Options Application See Listing 6.7 and 6.8

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Radio Buttons A radio button is used with other radio buttons to provide a set of mutually exclusive options Radio buttons have meaning only when used with one or more other radio buttons At any point in time, only one button of the group is selected (on) Radio buttons produce an action event when selected Radio buttons are defined by the JRadioButton class The ButtonGroup class is used to define a set of related radio buttons

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Quote Options Application See Listing 6.9 and 6.10

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sliders Sliders allow the user to specify a numeric value within a bounded range A slider can be presented either vertically or horizontally Optional features include –tick marks on the slider –labels indicating the range of values A slider produces a change event, indicating that the position of the slider and the value it represents has changed A slider is defined by the JSlider class

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Slide Colors Application See Listing 6.11 and 6.12

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Combo Boxes A combo box allows a user to select one of several options from a “drop down” menu When the user presses a combo box using a mouse, a list of options is displayed from which the user can choose A combo box is defined by the JComboBox class Combo boxes generate an action event whenever the user makes a selection from it

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Juke Box Application See Listing 6.13 and 6.14

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Timers Timers do not have a visual representation, but are a GUI component Timers are defined by the Timer class and are provided to help manage an activity over time A timer object generates an action event at regular intervals

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 – Rebound Application See Listing 6.15 and 6.16

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline GUI Elements More Components Layout Managers Mouse and Key Events Dialog Boxes Important Details GUI Design

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.7 – GUI Design Keep in mind our goal is to solve a problem Fundamental ideas of good GUI design include –knowing the user –preventing user errors –optimizing user abilities –being consistent We should design interfaces so that the user can make as few mistakes as possible

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Summary Chapter 6 focused on –discussing the core elements: components, events, and listeners –discussing the concept of a layout manager –examining mouse and keyboard events –describing tool tips and mnemonics –discussing GUI design issues