CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphical User Interfaces
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
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 Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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 Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
CPSC 233 Tutorial Xin Apr 6, Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Java Swing - Lecture 3 Layout Management
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces -- GUIs
Swing JComponents.
Modern Programming Language Java
Java GUI.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
GUI building with the AWT
Border Layout, using Panels, Introduction to PinBallGame
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout

Recap component listener event 1.User performs an action Button b = new Button(“Exit”); 2.An event object is created 0. A listener is created and added to the component b.addActionListener ( listener ); 3. The listener responses to the action public void actionPerformed (ActionEvent evt){ System.exit(0); }

Callback A component may add appropriate event listener objects Register themselves to JVM They will get callback when events addressing to them are generated Event listeners are defined through interfaces

interface An interface is purely abstract All methods are abstract, and the keyword is not needed No variables may be defined except constants Subtypes need to implement all methods Perfect for type definition You may implement the actionPerformed method in whatever way you need

Adapter For each of the event listener interfaces with more than a few methods defined, an adapter class is provided WindowListener  WindowAdapter A WindowAdapter object does nothing except you override the methods You need to override only those methods you care about E.g., windowClosing(WindowEvent env)

Inner Classes Classes implementing listener interfaces (or extending adapters) may be specific to a single container class A class like this may be treated as an inner class  declared and implemented within the container class itself When compiled, a separate.class file will be generated with a prefix of the container class

Adding Listeners to Components Multiple listeners may be added to one component One listener may be added to multiple components In the event handling methods, the source of the event need to be detected

Layout The layout of components within a container remains a mystery. Consider the ChangeColor2 frame: Why are the buttons placed side by side? Why are the buttons centered within the frame?

Layout Manager Classes The java.awt package provides five layout manager classes: Class Name Behavior BorderLayout Arranges components along the sides of the container and in the middle. CardLayout Arrange components in “cards.” Only one card is visible at a time. FlowLayout Arranges components in variable-length rows. GridBagLayout Aligns components horizontally and vertically; components can be of different sizes. GridLayout Arranges components in fixed-length rows and columns.

The FlowLayout Class Suppose that a frame containing seven buttons uses FlowLayout as its layout manager. The number of buttons that can be squeezed into a row depends on how wide the frame is:

The GridLayout Class The GridLayout layout manager places components in rows, with each row (except possibly the last) having an equal number of components:

The GridLayout Class If a frame with a GridLayout is resized, the components within the frame change size as well:

The GridLayout Class The GridLayout constructor requires that the number of rows and columns be specified: setLayout(new GridLayout(4, 5)); Components will be arranged in four rows and five columns, with no space between components.

The BorderLayout Class The positions in a BorderLayout are named North, South, East, West, and Center:

The BorderLayout Class The no-arg version of the BorderLayout constructor leaves no space between components: setLayout(new BorderLayout()); A different constructor is used if space is needed between components: setLayout(new BorderLayout(20, 10));

If a frame with a BorderLayout is resized, the heights of the “North” and “South” components don’t change: The widths of the “East” and “West” components also remain the same. The BorderLayout Class

Panels Consider the problem of creating a frame with the following appearance: