• No results found

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

N/A
N/A
Protected

Academic year: 2021

Share "Chapter 9: Graphical User Chapter 9: Graphical User Interfaces Interfaces"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 9: Graphical User Chapter 9: Graphical User

Interfaces Interfaces

Presentation slides for Presentation slides for

Java Software Solutions Java Software Solutions

Foundations of Program Design Foundations of Program Design

Second Edition Second Edition

by John Lewis and William Loftus by John Lewis and William Loftus

Java Software Solutions is published by Addison-Wesley Java Software Solutions is published by Addison-Wesley

Presentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved.

Presentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved.

Instructors using the textbook may use and modify these slides for pedagogical purposes.

Instructors using the textbook may use and modify these slides for pedagogical purposes.

(2)

Graphical User Interfaces Graphical User Interfaces

We can now explore the creation of graphical user We can now explore the creation of graphical user interfaces in more detail

interfaces in more detail

Chapter 9 focuses on: Chapter 9 focuses on:

GUI infrastructure GUI infrastructure

containers containers

using graphics in applications using graphics in applications

Swing components Swing components

layout managers layout managers

(3)

GUI Overview GUI Overview

To create a Java GUI, we need to understand: To create a Java GUI, we need to understand:

events events

listeners listeners

containers containers

components components

layout managers layout managers

special features special features

In Chapters 5 and 7 we introduced events and listeners, as In Chapters 5 and 7 we introduced events and listeners, as well as GUI components from the

well as GUI components from the java.awt package java.awt package

In this chapter we will focus on Swing components In this chapter we will focus on Swing components

(4)

AWT vs. Swing AWT vs. Swing

Early Java development used graphic classes defined in the Early Java development used graphic classes defined in the Abstract Windowing Toolkit (AWT)

Abstract Windowing Toolkit (AWT)

With Java 2, Swing classes were introduced With Java 2, Swing classes were introduced

Many AWT components have improved Swing counterparts Many AWT components have improved Swing counterparts

For example, the AWT For example, the AWT Button class corresponds to a more Button class corresponds to a more versatile Swing class called

versatile Swing class called JButton JButton

However, Swing does not generally replace the AWT; we However, Swing does not generally replace the AWT; we still use AWT events and the underlying AWT event

still use AWT events and the underlying AWT event processing model

processing model

(5)

Containers Containers

A container is a special component that can hold other A container is a special component that can hold other components

components

The AWT The AWT Applet Applet class, as well as the Swing class, as well as the Swing JApplet JApplet class, are containers

class, are containers

Other containers include: Other containers include:

panels panels

frames frames

dialog boxes dialog boxes

(6)

Graphics in Applications Graphics in Applications

Applets must be displayed through a browser or through Applets must be displayed through a browser or through the appletviewer

the appletviewer

Similarly, a panel must be displayed within the context of Similarly, a panel must be displayed within the context of another container

another container

A frame is a container that is free standing and can be A frame is a container that is free standing and can be positioned anywhere on the screen

positioned anywhere on the screen

Frames give us the ability to do graphics and GUIs through Frames give us the ability to do graphics and GUIs through applications (not just applets)

applications (not just applets)

(7)

Window Events Window Events

Because a frame is a free standing window, we must now Because a frame is a free standing window, we must now address

address window events window events

Specifically, we must be able to handle a Specifically, we must be able to handle a window closing window closing event

event

Frames have an icon in the corner of the window to close it Frames have an icon in the corner of the window to close it

Clicking it will cause the Clicking it will cause the windowClosing windowClosing method of a method of a window listener object to be invoked

window listener object to be invoked

See GenericWindowListener.java (page 412) See GenericWindowListener.java (page 412)

See ShowFrames.java (page 413) See ShowFrames.java (page 413)

(8)

Swing Components Swing Components

There are various Swing GUI components that we can There are various Swing GUI components that we can incorporate into our software:

incorporate into our software:

labels (including images) labels (including images)

text fields and text areas text fields and text areas

buttons buttons

check boxes check boxes

radio buttons radio buttons

menus menus

combo boxes combo boxes

and many more… and many more…

Using the proper components for the situation is an Using the proper components for the situation is an important part of GUI design

important part of GUI design

(9)

Labels and Image Icons Labels and Image Icons

A label is used to provide information to the user or to add A label is used to provide information to the user or to add decoration to the GUI

decoration to the GUI

A Swing label is defined by the A Swing label is defined by the JLabel JLabel class class

It can incorporate an image defined by the It can incorporate an image defined by the ImageIcon ImageIcon class

class

The alignment and relative positioning of the text and The alignment and relative positioning of the text and image of a label can be explicitly set

image of a label can be explicitly set

See ShowLabels.java (page 416) See ShowLabels.java (page 416)

See LabelDemo.java (page 417) See LabelDemo.java (page 417)

(10)

Buttons Buttons

GUI buttons fall into various categories: GUI buttons fall into various categories:

push button – a generic button that initiates some action push button – a generic button that initiates some action

check box – a button that can be toggled on or off check box – a button that can be toggled on or off

radio buttons – a set of buttons that provide a set of mutually radio buttons – a set of buttons that provide a set of mutually exclusive options

exclusive options

Radio buttons must work as a group; only one can be Radio buttons must work as a group; only one can be toggled on at a time

toggled on at a time

Radio buttons are grouped using the Radio buttons are grouped using the ButtonGroup ButtonGroup class class

(11)

Buttons Buttons

Push buttons and radio buttons generate action events Push buttons and radio buttons generate action events when pushed or toggled

when pushed or toggled

Check boxes generate Check boxes generate item state changed item state changed events when events when toggled

toggled

See Quotes.java (page 419) See Quotes.java (page 419)

See QuotesControls.java (page 420) See QuotesControls.java (page 420)

(12)

Combo Boxes Combo Boxes

A A combo box combo box displays a particular option with a pull down displays a particular option with a pull down menu from which the user can choose a different option menu from which the user can choose a different option

The currently selected option is shown in the combo box The currently selected option is shown in the combo box

A combo box can be A combo box can be editable editable , so that the user can type their , so that the user can type their option directly into the box

option directly into the box

See JukeBox.java (page 425) See JukeBox.java (page 425)

See JukeBoxControls.java (page 426) See JukeBoxControls.java (page 426)

(13)

Layout Managers Layout Managers

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

in which components are displayed in a container

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

the Java standard class library:

Defined in the AWT Defined in the AWT

Defined in Swing Defined in Swing Flow Layout

Flow Layout Border Layout Border Layout Card Layout Card Layout Grid Layout Grid Layout

GridBag Layout GridBag Layout Box Layout

Box Layout

Overlay Layout

Overlay Layout

(14)

Layout Managers Layout Managers

Every container has a default layout manager, but we can Every container has a default layout manager, but we can also explicitly set the layout manager for a container

also explicitly set the layout manager for a container

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

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

preferred size or alignment, and others do not

The layout managers attempt to adjust the layout as The layout managers attempt to adjust the layout as

components are added and as containers are resized

components are added and as containers are resized

(15)

Flow Layout Flow Layout

A flow layout puts as many components on a row as A flow layout puts as many components on a row as possible, then moves to the next row

possible, then moves to the next row

Rows are created as needed to accommodate all of the Rows are created as needed to accommodate all of the components

components

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

the container

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

can be explicitly set

(16)

Border Layout Border Layout

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

can be added

North

South

Center East

West

(17)

Border Layout Border Layout

Each area displays one component (which could be another Each area displays one component (which could be another container)

container)

Each of the four outer areas enlarge as needed to Each of the four outer areas enlarge as needed to accommodate the component added to them

accommodate the component added to them

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

and other areas expand to fill the void

The center area expands to fill space as needed The center area expands to fill space as needed

(18)

Box Layout Box Layout

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

one row) or vertically (in one column)

Special rigid areas can be added to force a certain amount Special rigid areas can be added to force a certain amount of spacing between components

of spacing between components

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

different configurations can be created

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

one container that uses the more complicated gridbag layout manager

layout manager

(19)

Special Features Special Features

Swing components offer a variety of other features Swing components offer a variety of other features

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

mouse cursor rests momentarily on a component

Borders Borders around each component can be stylized in various around each component can be stylized in various ways ways

Keyboard shortcuts called Keyboard shortcuts called mnemonics mnemonics can be added to can be added to graphical objects such as buttons

graphical objects such as buttons

(20)

GUI Design GUI Design

In addition to the tools necessary to put a GUI together, we In addition to the tools necessary to put a GUI together, we must also focus on solving the problem

must also focus on solving the problem

The GUI designer should: The GUI designer should:

Know the user and their needs Know the user and their needs

Prevent user errors whenever possible Prevent user errors whenever possible

Optimize user abilities and make information readily available Optimize user abilities and make information readily available

Be consistent with placement of components and color schemes Be consistent with placement of components and color schemes

References

Related documents

Pursuant to profiling suicides and undetermined cases, we used a generalized linear mixed model (GLMM) to test the respective hypotheses that: among the pooled cases, the designation

nRHO-1  induces the release of the neurotransmitter ACh from syn- aptic vesicles [18] but, ACh release is also stimulated in dgk-1 mutants [ 18] and pkc-1(gf) animals [61] neither

experimentally evaluate the Page Rank-based perfecting mechanism. In particular, we chose September 2000 log as a representative one for our experiment. The experiment consisted

Recommend remedies and/or alternative service excavation support methods to address inappropriate mining support methods and/or strategies, their mining ramifications and

Su trabajo ha sido recogido en publicaciones internacionales, como C3 (Corea), Temas de Arquitectura (España) o PAR (Perú).Ha sido profesor visitante de proyectos arquitectónicos

Where a combined hepatitis A and typhoid vaccine has been used to initiate immunisation, a dose of single antigen hepatitis A vaccine will be required six to 12 months later in

A real graphical user interface includes window frames which you create that contain buttons, text input fields, and other onscreen components.. A major part of creating a

The risk data for each structure contain the following: average annualized loss, flood depths for multiple return periods, first floor elevation, low entry elevation, and chance