java programming: advanced topics 1 common elements of graphical user interfaces chapter 6

44
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Upload: eunice-stephens

Post on 13-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

1

Common Elements of Graphical User Interfaces

Chapter 6

Page 2: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

2

Objectives

• Learn the characteristics of a graphical user interface

• Explore the contents of the Java Foundation Classes

• Discover how the JFC supports creating GUIs for the Java platform

• Design GUIs with layouts that dynamically adjust to screen resolution and window size

• Include predefined components such as text fields, buttons, and multimedia elements in your GUI

Page 3: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

3

Objectives (Cont.)

• See how the JFC provides a framework for interacting with the user in event-driven programs

• Write code using component, container, and layout manager classes of the AWT and Swing

• State the name of the event class for any specified listener interface in the java.awt.event package

• Write code to implement the paintComponent method of a javax.swing.Jcomponent component

Page 4: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

4

Main Features and Terminology of a GUI

• Components are predefined standard elements (such as buttons, text fields, frame windows, and dialog boxes)

• The layout managers control the way components are arranged within a container

• Graphics: drawing in the graphics context for a component

• Graphics context: is the area on the display screen where a component is positioned

Page 5: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

5

Main Features and Terminology of a GUI (Cont.)

• Painting: the process of displaying components or refreshing the display of components on the screen

• Events can be triggered by the operating system or by user actions

• The adapter is a class that implements all the methods of the interface by providing empty methods for all of them

• Each listener interface that has more than one method also has a corresponding adapter class

Page 6: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

6

Introducing the Java Foundation Classes

• The goal of the JFC is to simplify the development of 100% Pure Java programs for network or standalone environments

• The classes enable you to:– Design GUIs that reflect the operating system that is

host to the JVM– Create your own platform-independent interface – Use a look and feel defined for the Java platform and

common across all implementations of the JVM

Page 7: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

7

Introducing the Java Foundation Classes

Page 8: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

8

Introducing the Java Foundation Classes

Page 9: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

9

Abstract Windowing Toolkit API

Page 10: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

10

Abstract Windowing Toolkit API (Cont.)

Page 11: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

11

Abstract Windowing Toolkit API (Cont.)

Page 12: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

12

Event Model• Define any class to be an event handler by

implementing a listener interface• Call a method of a component class to register

the listener with the component• When an event that relates to the component

occurs, only the registered listeners are notified and passed a copy of the event

• The event model is consistent with the JavaBeans interface

Page 13: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

13

Swing API• The Swing API consists of more than 250

classes and 75 interfaces grouped into the following categories:– JComponent: extends the AWT class

Container and a set of components that are subclasses of JComponent

– Nonvisible support classes that provide important services

– A set of related interfaces that are implemented by Swing component and support classes

Page 14: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

14

Swing Components

Page 15: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

15

Swing Components (Cont.)

Page 16: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

16

Swing Components (Cont.)

Page 17: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

17

Swing Components (Cont.)

Page 18: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

18

Separable Model Architecture• Swing components separate the manipulation of

data associated with a component from the rendering or graphical-representation component and its contents

• The Swing API uses a separable model architecture

• Swing models use two different styles of notification: – Lightweight– Stateful

• The Swing API defines a set of model interfaces

Page 19: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

19

Swing Model Interfaces

Page 20: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

20

Swing Model Interfaces (Cont.)

Page 21: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

21

An Example of JFC Program

Page 22: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

22

Layout Managers• Layout managers automate the positioning of

components within containers• Standard layout managers:

– BorderLayout– FlowLayout– GridLayout– GridBagLayout– BoxLayout– CardLayout

• The javax.swing.JTabbedPane and javax.swing.JSplitPane containers in the Swing API have built-in layout characteristics

Page 23: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

23

Layout Managers and Related Interfaces

Page 24: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

24

Border Layouts• The BorderLayout class implements the

LayoutManager2 interface to support a container that holds up to five components:– North– South– West– East– Center

• By default, the JWindow, JFrame, and JDialog containers have border layouts

Page 25: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

25

The BorderLayout Strategy

Page 26: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

26

Flow Layouts

• The class FlowLayout implements the LayoutManager interface

• Components are arranged in a row across the area of the container

• Components in a flow layout retain their preferred size

• By default, JPanel objects have flow layouts

Page 27: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

27

A FlowLayout Example

Page 28: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

28

Grid Layouts• The class GridLayout implements the

LayoutManager2 interface• GridLayout manager divides the area of the

container into a grid of equally sized rows and columns

• Components are automatically put into the next cell, in a left to right order, filling rows from top to bottom

Page 29: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

29

A GridLayout Example

Page 30: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

30

Grid-Bag Layouts• The class GridBagLayout implements the

LayoutManager2 interface to support grid-bag layouts

• A grid-bag layout is based on a rectangular grid, and it uses a helper class, GridBagConstraints, to specify how each component should be located within the grid

Page 31: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

31

A GridBagLayout Example

Page 32: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

32

Events• Events: objects that encapsulate changes in

state that are initiated by the user or the operating system

• The AWT API includes the package java.awt.event of the classes that encapsulate the events that relate to the GUI components in your application or applet

Page 33: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

33

AWT Event Classes

Page 34: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

34

AWT Event Classes(Cont.)

Page 35: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

35

Swing Event Classes

Page 36: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

36

Selected Methods of the Event Classes

Page 37: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

37

Selected Methods of the Event Classes (Cont.)

Page 38: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

38

Events that Start Long Operations

Page 39: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

39

Painting• Painting: the act of drawing onto the graphics

context of a component• All objects that are instances of subclasses of

java.awt.Component handle their own painting• The key methods:

– void paintComponent (Graphics context)– void paint (Graphics context)– void repaint (long time)– void update (Graphics context)

Page 40: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

40

Basic Support for Graphics• The AWT API provides basic graphics support

through the class Graphics• The drawing methods of the class Graphics are

instance methods of the Graphics class, and each instance of Graphics is the graphics context for a component

• The Graphics class provides a number of methods for drawing onto a component

Page 41: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

41

Java 2D API • The basic support for drawing was lacking for

high-quality graphics• The goal was to create an API that addressed

the needs of graphics professionals• The Java 2D API was created and included as

part of the JFC

Page 42: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

42

Notable Java 2D Features

Page 43: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

43

Summary• The JFC consists of five major APIs, including

the classes and interfaces in the AWT and Swing APIs that you use to program a GUI for an application or applet

• Layout managers are used to arrange the components in a way that adjusts for screen resolutions and window resizings

• Standard layout managers include BorderLayout, FlowLayout, GridLayout, GridBagLayout, Box, and CardLayout

• Each component class supports a number of events that are appropriate to its type of component

Page 44: Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6

Java Programming: Advanced Topics

44

Summary (Cont.)

• The AWT contains a hierarchy of event classes for different kinds of user-initiated events, such as ComponentEvent, MouseEvent, KeyEvent, ItemEvent, TextEvent, and ActionEvent

• Each listener interface that has more than one method also has a corresponding adapter class

• Painting is the act of drawing onto the graphics context of a component, and drawing methods of the Graphics class are paintComponent, paint, and update methods of a component

• The goal of the Java 2D API addresses the needs of graphics professionals