+ All Categories
Home > Documents > Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides...

Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides...

Date post: 19-Dec-2015
Category:
View: 218 times
Download: 1 times
Share this document with a friend
Popular Tags:
46
Chapter 9: Graphical User Chapter 9: Graphical User Interfaces Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used with permission. All rights reserved.
Transcript
Page 1: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Chapter 9: Graphical User Chapter 9: Graphical User Interfaces Interfaces

Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and

William Loftus, 2002, and used with permission. All rights reserved.

Page 2: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

2

Graphical User InterfacesGraphical User Interfaces

Now we can explore the creation of graphical user interfaces in more detail

Chapter 9 focuses on:

• GUI design guidelines• layout managers• nested containers for organizing components• borders, tool tips, mnemonics, and other special

features• additional GUI components and events

Page 3: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

GUI OverviewGUI Overview

To create a Java GUI, we need to understand:• events• listeners• components• containers• listener interfaces and adapter classes• layout managers• special features

In this chapter we will build on ideas presented in the graphics tracks of previous chapters

Page 4: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

GUI DesignGUI Design

We must remember that our goal is to solve the problem using the tools needed to put a GUI together

The GUI designer should:

• Know the users and their needs

• Prevent user errors whenever possible

• Optimize user abilities and make information readily available

• Be consistent with placement of components and color schemes

Page 5: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Layout ManagersLayout Managers

A layout manager is an object that determines the manner in which components are arranged in a container

There are several predefined layout managers defined in the Java standard class library:

Defined in the AWT

Defined in Swing

Flow LayoutBorder LayoutCard LayoutGrid LayoutGridBag LayoutBox Layout

Overlay Layout

Page 6: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Layout ManagersLayout Managers

Every container has a default layout manager, but we can explicitly set the layout manager as well

Each layout manager has its own particular rules governing how the components will be arranged

Some layout managers pay attention to a component's preferred size or alignment, while others do not

A layout manager attempts to adjust the layout as components are added and as containers are resized

Page 7: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Layout ManagersLayout Managers

We can use the setLayout method of a container to change its layout manager

JPanel panel = new JPanel();

panel.setLayout (new BorderLayout());

The following example uses a tabbed pane, a container which permits one of several panes to be selected

See LayoutDemo.java (page 512)

See IntroPanel.java (page 514)

Page 8: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

LayoutDemo - IntroLayoutDemo - Intro

Page 9: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Flow LayoutFlow Layout

Flow layout puts as many components as possible on a row, and then moves to the next row

Rows are created as needed to accommodate all of the components

Components are displayed in the order they are added to the container

Each row of components is centered horizontally in the window by default, but could also be aligned left or right

The horizontal and vertical gaps between the components can be explicitly set also

See FlowPanel.java (page 515)

Page 10: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

LayoutDemo - FlowLayoutDemo - Flow

Page 11: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Border LayoutBorder Layout

A border layout defines five areas to which components can be added

North

South

Center EastWest

Page 12: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Border LayoutBorder Layout

Each area displays one component (which could be another container such as a JPanel)

Each of the four outer areas enlarges as needed to accommodate the component added to it

If nothing is added to the outer areas, they take up no space and other areas expand to fill the void

The center area expands to fill space as needed

See BorderPanel.java (page 518)

Page 13: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

LayoutDemo - BorderLayoutDemo - Border

Page 14: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Grid LayoutGrid Layout

A grid layout presents a container’s components in a rectangular grid of rows and columns

One component is placed in each cell of the grid, and all cells have the same size

As components are added to the container, they fill the grid from left-to-right and top-to-bottom (by default)

The size of each cell is determined by the overall size of the container

See GridPanel.java (page 521)

Page 15: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

LayoutDemo - GridLayoutDemo - Grid

Page 16: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Box LayoutBox Layout

A box layout organizes components either horizontally (in one row) or vertically (in one column)

Components are placed top-to-bottom or left-to-right in the order in which they are added to the container

By combining multiple containers using box layout, many different configurations can be created

Multiple containers with box layouts are often preferred to one container that uses the more complicated gridbag layout manager

Page 17: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Box LayoutBox Layout

Invisible components can be added to a box layout container to take up space between components

• Rigid areas have a fixed size

• Glue specifies where excess space should go

A rigid area is created using the createRigidArea method of the Box class

Glue is created using the createHorizontalGlue or createVerticalGlue methods

See BoxPanel.java (page 524)

Page 18: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

LayoutDemo - BoxLayoutDemo - Box

Page 19: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Containment HierarchiesContainment Hierarchies

The way components are grouped into containers and the way those containers are nested within each other establishes the containment hierarchy for the GUI

Each container can have its own layout manager

The appearance of a GUI is determined by:

• the containment hierarchy

• the layout manager of each container

• the properties of individual components

All of these issues work together to determine the final visual effect

Page 20: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Containment HierarchiesContainment Hierarchies

Page 21: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Special FeaturesSpecial Features

Swing components offer special features to facilitate and enhance their use

Special Feature

Description

Tool tip Causes a line of text to appear when the mouse cursor pauses over a component

Mnemonic Allows an action to occur in response to a keyboard key combination

Disable Allows a component to be explicitly enabled or disabled

Border Surrounds a component with a border

Page 22: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Tool TipsTool Tips

Tool tips provide a short pop-up description when the mouse cursor rests momentarily on a component

A tool tip is assigned using the setToolTipText method of a Swing component

JButton button = new JButton ("Compute");

button.setToolTipText ("Calculate size.");

Page 23: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

MnemonicsMnemonics

A mnemonic provides a keyboard alternative for pushing a button or selecting a menu option

The mnemonic character should be chosen from the component's label, and is underlined

The user activates the component by holding down the ALT key and pressing the mnemonic character

A mnemonic is established using the setMnemonic method

JButton button = new JButton ("Calculate");

button.setMnemonic ("C");

Page 24: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Disabled ComponentsDisabled Components

Components can be disabled if they should not be used

A disabled component is "grayed out" and will not respond to user interaction

The status is set using the setEnabled method:

JButton button = new JButton (“Do It”);

button.setEnabled (false);

Page 25: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Special FeaturesSpecial Features

The right combination of special features and components can enhance the usefulness of a GUI

See LightBulb.java (page 530)

See LightBulbPanel.java (page 531)

See LightBulbControls.java (page 533)

Page 26: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The LightBulb ProgramThe LightBulb Program

Page 27: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

BordersBorders

A border can be put around any Swing component to define how the edges of the component should be drawn

The BorderFactory class is useful for creating border objects

It has methods for creating specific types of borders

A border is applied to a component using the setBorder method:

JPanel myPanel = new JPanel();

Border myBorder = BorderFactory.createEtchedBorder();

myPanel.setBorder(myBorder);

Page 28: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

BordersBorders

An empty border

• buffers the space around the edge of a component

• otherwise has no visual effect

A line border

• surrounds the component with a simple line

• the line's color and thickness can be specified

An etched border

• creates the effect of an etched groove around a component

• uses colors for the highlight and shadow

Page 29: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

BordersBorders

A bevel border

• can be raised or lowered

• uses colors for the outer and inner highlights and shadows

A titled border

• places a title on or around the border

• the title can be oriented in many ways

A matte border

• specifies the sizes of the top, left, bottom, and right edges of the border separately

• uses either a solid color or an image

Page 30: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

BordersBorders

A compound border

• is a combination of two borders

• one or both of the borders can be a compound border

See BorderDemo.java (page 536)

Page 31: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The BorderDemo ProgramThe BorderDemo Program

Page 32: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Scroll PanesScroll Panes

A scroll pane is useful for images or information too large to fit in a reasonably-sized area

A scroll pane offers a limited view of the component it contains

It provides vertical and/or horizontal scroll bars that allow the user to scroll to other areas of the component

No event listener is needed for a scroll pane

See TransitMap.java (page 540)

Page 33: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The TransitMap ProgramThe TransitMap Program

Page 34: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Split PanesSplit Panes

A split pane (JSplitPane) is a container that displays two components separated by a moveable divider bar

The two components can be displayed side by side, or one on top of the other

Moveable Divider Bar

LeftComponent

RightComponent

Top Component

Bottom Component

Page 35: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Split PanesSplit Panes

The orientation of the split pane is set using the HORIZONTAL_SPLIT or VERTICAL_SPLIT constants

The divider bar can be set so that it can be fully expanded with one click of the mouse

The components can be continuously adjusted as the divider bar is moved, or wait until it stops moving

Split panes can be nested

Page 36: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

ListsLists

The Swing Jlist class represents a list of items from which the user can choose

The contents of a JList object can be specified using an array of objects

A JList object generates a list selection event when the current selection changes

See PickImage.java (page 544)

See ListPanel.java (page 546)

Page 37: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The PickImage ProgramThe PickImage Program

Page 38: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

ListsLists

A JList object can be set so that multiple items can be selected at the same time

The list selection mode can be one of three options:

• single selection – only one item can be selected at a time

• single interval selection – multiple, contiguous items can be selected at a time

• multiple interval selection – any combination of items can be selected

The list selection mode is defined by a ListSelectionModel object

Page 39: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Combo BoxesCombo Boxes

A combo box provides a menu from which the user can choose one of several options

The currently selected option is shown in the combo box

Unlike a JList object, a combo box shows its options only when the user presses it using the mouse

Options can be established using an array of strings or using the addItem method

A combo box generates an action event when the user makes a selection from it

Page 40: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The JukeBox ProgramThe JukeBox Program

See JukeBox.java (page 550)

See JukeBoxControls.java (page 551)

Page 41: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

SlidersSliders

A slider is a component that allows the user to specify a numeric value within a bounded range

The JSlider class has several methods that allow the programmer to tailor the look of a slider

Major and minor tick marks, and labels on the tick marks, can be explicitly set and displayed

A slider produces a change event

See ViewColors.java (page 554)

See ViewSliderPanel.java (page 556)

Page 42: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

The ViewColors ProgramThe ViewColors Program

Page 43: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

Events RevisitedEvents Revisited

Some events are generated only by certain components

But we can set up a listener on any component for any of the following events:

• component event - changing a component's size, position, or visibility

• focus event - gaining or losing the keyboard focus

• key event - pressing, releasing, or clicking keyboard keys

• mouse event - clicking the mouse button and moving the mouse onto and off of a component

• mouse motion event - moving or dragging a mouse over a component

Page 44: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

More About GUIsMore About GUIs

We've only scratched the surface of GUIs – but we've established the core issues

There are several other components and events to use

Java supports menus and submenus

A tool bar is a container that groups several components into a row or column

An internal frame is a container that operates like a regular frame, but only within another window

Page 45: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

More About GUIsMore About GUIs

A layered panel is a container that takes depth into consideration

A progress bar indicates the progress of an activity

A table displays data in table format

A tree presents data in a hierarchical format

Java also provides rich support for text processing

Page 46: Chapter 9: Graphical User Interfaces Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,

SummarySummary

Chapter 9 has focused on:

• GUI design guidelines• layout managers• nested containers for organizing components• borders, tool tips, mnemonics, and other special

features• additional GUI components and events


Recommended