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
Introduction to Java 2 Programming
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
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.
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.
1 Chapter 4 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
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)
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
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.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
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.
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, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 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.
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, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
IM103 week 11 Part 2 P532 Case Study part 2: the Airport GUI Learning objectives By the end of this lecture you should be able to:  use the JTabbedPane.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
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,
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Chapter 141 More Swing Chapter Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Chapter 14- More Swing, Better looking applications.
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, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3: I Need a Tour Guide (Introduction to Visual Basic 2012)
Advanced User Interfaces
Graphical User Interface (GUI) Programming III
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
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 More Swing Chapter 15

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 Add menus, icons, borders, and scroll bars to your GUIs Use the BoxLayout manager and the Box class Use inner classes and describe their advantages Use the WindowListener interface Create GUIs whose components change from visible to invisible and vice versa

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 GUI with a Menu Menu items behave like buttons Note example program, listing 15.1 class MemoGUIexample 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 Menu Bars, Menus, and Menu Items A menu item is a JMenuItem object A menu is a JMenu object  Use the.add method to add a menu item to a menu A menu bar is a JMenuBar object and a container for menus  Add menu bar to a frame with setJMenuBar

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 Nested Menus Class JMenu is a descendant of the JMenuItem class Every menu is also a menu item object Thus a menu can be a menu item in another menu

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 Icons An icon is  A picture  An object of ImageIcon An icon can be placed in  A label  A button  Both can also have text with the icon

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 Using Icons View sample program, listing 15.2 class IconDemosample 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 Swing Methods Figure 15.1a some methods in the classes JButton and JLabel

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 Swing Methods Figure 15.1b some methods in the classes JButton and JLabel

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 Swing Methods Figure 15.1c some methods in the classes JButton and JLabel

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 Area Features Figure 15.2 View port for a text area

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 JScrollPane Class for Scroll Bars A view port shows a portion of the class A scroll pane is a viewport with scrollbars Vertical and/or horizontal scrollbars may be specified

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 Example Program View code, listing 15.3 class ScrollBarDemocode 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 JScrollPane methods Figure 15.3 Some methods and constants in class JScrolPane

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 Borders View sample program, listing 15.4 class BorderDemosample 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 Border Class Constructors Figure 15.4a, some constructors of various border 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 Border Class Constructors Figure 15.4b, some constructors of various border 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 BoxLayout Manager Class View demo program, listing 15.5 class BoxLayoutDemodemo 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 Struts and Glue A strut is an invisible component  A fixed length used to separate visible components  Note use of strut in listing 15.5strut in listing  Struts can be either horizontal or vertical Glue is also an invisible component  Not used to stick things together  Rather, they are not rigid, more like "goo"  Glue components also separate visible items

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 Setting Spacing Between Components Methods used to set horizontal and vertical spacing Except for classes Box and BoxLayout

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 Box Container Class View demo program, listing 15.6 class BoxClassDemodemo 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 The CardLayout Manager View demo program, listing 15.7 class CardLayoutDemodemo program The CardLayout manager creates a set of views that can be cycled Do not use an anonymous argument when using the CardLayout manager

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 CardLayout Manager Initially or after reset button clicked After clicking red button After clicking green button 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 CardLayout Methods Figure 15.5 Some methods in the CardLayout manager 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 Inner Classes Classes defined within other classes Often used in Swing programs and classes  However not confined to that usage Advantages  Can be used to make outer class self contained  Methods of inner class have access to all elements of the outer 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 Helping Classes View example, listing 15.8 class InnerClassDemoexample 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 The WindowListener Interface View demo, listing 15.9 class WindowListenerDemodemo 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 the Close-Window Button View example, listing class CloseWindowDemoexample By default a window's close-window button does not end the execution It is possible to program the behavior of a window's close window button

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 the Close-Window Button Click the close-window button  The second window appears 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 Interface WindowConstants Figure 15.6 Constants in the interface WindowConstants

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 Components with Changing Visibility View sample program, listing class VisibilityDemosample program After clicking the Up button After clicking the Down button 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, Menus, Abstract Buttons Figure 15.7 A portion of the Swing Class Hierarchy

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, Menus, Abstract Buttons Figure 15.8 Some methods inherited from AbstractButton by class JButton and JMenuItem

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, Menus, Abstract Buttons Figure 15.9 Some constructors of classes JButton and JMenuItem

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 Classes JMenuItem, JMenu, and JMenuBar provide menu tools You can add icons to buttons, labels, menu items Menu bar can be added to a frame by using  setJmenu  add Buttons and menu items need registered 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 Summary Close-window button can be reprogrammed like any other button  May need to call setDefaultCloseOperation Changing visibility of a component requires call to method validate