Upload
whitney-james
View
215
Download
2
Embed Size (px)
Citation preview
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming.
AWT features include: a rich set of user interface components;
a robust event-handling model; graphics and imaging tools, including shape,
color, and font classes; layout managers, for flexible window layouts that don't depend on a particular window size
or screen resolution; data transfer classes, for cut-and-paste through
the native platform clipboard.
AWT Hierarchy
AWT Overview
• Components– UI objects, such as buttons and labels
• Graphics Classes– Represents a “Drawing Context”– All drawing is done through this class
• Event Classes– Used to inform the program of user events (mouse,
keyboard, etc)
• Image Class– Used to load, display and store images
Anatomy of an AWT Applet
• Subclasses Applet
• Creates Components to provide basic controls
• Using Containers and Layout Managers, it groups the Components
• User actions result in events– events are delivered to “event listeners”
AWT Overview
• Components– UI objects, such as buttons and labels
• Graphics Classes– Represents a “Drawing Context”– All drawing is done through this class
• Event Classes– Used to inform the program of user events (mouse,
keyboard, etc)
• Image Class– Used to load, display and store images
Event Handling
• Events are generated by event sources
• One or more listeners can register to be notified about events of a particular kind from a particular source
• Event handlers can be instances of any class, as long as they implement an event listener interface
Laying out Components
• Layout managers control:– Where components appear– What sizes they are– How they react when they are resized
• The basic layout managers are:– BorderLayout (default for windows)– FlowLayout (default for panels)– GridLayout
• You can also use Absolute Positioning
BorderLayout
• It uses five areas to hold components: north, south, east, west, and center.
• All extra space is placed in the center area.
FlowLayout
• It simply lays out components from left to right, starting new rows if necessary
BorderLayout
• It uses five areas to hold components: north, south, east, west, and center.
• All extra space is placed in the center area.
FlowLayout
• It simply lays out components from left to right, starting new rows if necessary
CardLayout
• Use the CardLayout class when you have an area that can contain different components at different times
Using the Default Layout
• If you want to use the default layout manager, you don't have to do a thing. The constructor for each Container creates a layout manager instance and initializes the Container to use it.– All Panels (including Applets) default to
FlowLayout– All Windows default to BorderLayout
Using a Non-Default Layout
• You need to create an instance of the desired layout manager class and tell the Container to use it.
• This code creates a CardLayout manager and sets it up as the layout manager for a Container:aContainer.setLayout(new CardLayout());
Using Graphics Overview
• “Graphics” objects are the key to all drawing
• They support primitive graphics ...– Lines, Rectangles, Text
• … and Images
• They also store the “drawing context”:– Current drawing area– Current Drawing Color
Using Graphics: Drawing
• The AWT drawing system controls when and how programs can draw
• In response to a component’s repaint() method being called, the AWT invokes the Component's update()
• update() in turn (by default) , calls the component’s paint() method.