14
10.1 AWT

10.1 AWT. 10.1.1 The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform

Embed Size (px)

Citation preview

10.1 AWT

10.1.1 The AWT classes

• Users today expect a Graphical User Interface (GUI)

• Improves application usability• Difficult to implement cross-platform GUIs

10.2.1 Designing the class

• Design the GUI on paper. Get Feedback• Identify the components required, including

containers• Select a Layout manager for each container• Create and add components to the containers.

Treat them as a hierarchy. Start with the lowest level first

• Register listeners as appropriate• Set the highest-level container size, and make

it visible

10.2.2 Creating the components

• Button– Viewable on-screen object, uses events to report

useage

• Label– A viewable string of text

• TextArea– A viewable area that can hold multiple lines of text

• TextField– A viewable field that can hold one line of text

10.2.3 Selecting containers

• Two main types of containers – Window & Panel

• Frame & Dialog are subclasses of Window– Frame is a window with a title, and is resizable– Dialog is movable but not resizable

• Panel requires a container, and can contain other elements

• Panels are used to arrange groups of elements on the same container (eg, Frame or Dialog)

10.2.4 Layout managers

• Layout managers arrange components in a container

• Relative arrangement, rather than absolute

• Because Java is cross-platform, differences exist

• Require relational layout to ensure GUI is usable on multiple platforms

10.2.5 Sizing components and containers

• Layout manager positions components in the container

• When a container needs to arrange its components it uses its reference to the layout manager

• Resizing a container also invokes the layout manager

• Layout manager can control the size of components

• Size the highest level container once all components have been added

• Make it visible using setVisible(true)

10.3.1 Event model • Components that react to user input can generate

events• Events are objects that represent what happened• Objects that are designed to process the event register

as a listener with the event generator

10.3.2 Events & listeners

• Event objects represent the event

• Subclass from java.awt.AWTEvent– ActionEvent, TextEvent, KeyEvent, InputEvent

• Objects designed to process events are called handlers

• Handlers implement the appropriate listener interface

• Register with the event generator

10.3.3 Listener classes

• Separate listener interface for each event type

• ActionListener, ItemListener, FocusListener, etc

• The class that will be receiving the event implements the appropriate listener

• The action is performed in the method specified by the interface

10.3.4 Adapter classes

• Some listener interfaces include many methods

• To implement these interfaces the handler class must implement all these methods

• Java provides adapter classes, that implement empty methods

• The handler need only extend the adapter class and implement the method(s) of interest

10.3.5 Inner classes & anonymous classes

• Some classes can be defined within other classes

• Eliminates need for a separate source file

• Often used for listeners or adapters

10.3.6 Anonymous classes • Defined within a

statement• Has no class

name or explicit reference

• Usually a simple implementation

• Can extend a class OR implement ONE interface

10.4 Case Study: JBANK Application