
|
|
3. AWT Review
- This course assumes familiarity with AWT
- This review will be fast!
- Concentrate on layout managers and interface design
- AWT = Abstract Windowing Toolkit
- Component = basic GUI element in AWT
- Container = a component that can contain other components
- Layout Manager = a class that figures out component position/size
- Event = a external stimulus to an object
- Highest level AWT element
- All other AWT elements are extended from this
- Abstract
- Can extend directly from this to create a "lightweight" component
- Most functionality is defined here
- Can contain other AWT elements
- Abstract
- Can extend directly from this to create a "lightweight" container
- If extending, override addImpl() to track additions
- Can have layout manager
- Graphical representation of a push button
- Clicking it dispatches an ActionEvent event.
- Can be decorated using its paint() method.
- Is usually the class extended when creating a custom component.
- A text label with a box that can be checked/unchecked
- Has state of true = checked, false = unchecked
- When associated with Checkboxes, the Checkboxes become "radio buttons"
- Only one Checkbox in a given CheckboxGroup can be checked at a time
- Presents a selected item.
- Clicking the down arrow opens a list of selections to choose from.
- Selected item cannot be edited (not a "ComboBox").
- A piece of text written to the screen.
- More useful than just graphically drawing text, as it can be positioned by a layout manager.
- Presents a list of text items.
- One or more may be selected depending on its setting.
- Visually displays a range,and a position within that range,
- Provides common functionality for TextArea and TextField
- Allows user to enter a block of text.
- Primitive editing functions provided.
- 32K limit on most platforms.
- Don't use this if you're writing an editor!
- A container (can contain other components)
- Provides a canvas-like area on which graphics can be drawn.
- Note that it is not a Canvas
- Is a good candidate for creating new container components that need graphics drawn on them.
- A panel (so it's a container as well...)
- Can be displayed in a web page.
- A container that provides scrollbars to move around its contained component.
- It is only allowed to contain one component.
- But that component could be a container...
- A native window on the current platform
- Does not have window decorations like
- border
- title bar
- system menu
- minimize/maximize/close buttons
- A window that can be manipulated as a standard application window on the target platform.
- Has all standard window decorations for the platform.
- Is the only component that can contain a menubar!
- A frame-like window that is used for requesting information from the user.
- Can be modal or non-modal.
- Read dialogs are extended from this parent class.
- A standard dialog that can be used to load or save files.
- Uses the platform's native file dialog(s).
- Provide a hierarchical means for the user to select an action to perform.
- Items in a menu can themselves be menus
- Items can be checkbox-like items.
- Several components that work together:
- MenuBar -- attaches to a Frame to hold Menus.
- Menu -- the high-level name in the menu bar. Can contain other Menus, MenuItems, CheckboxMenuItems or MenuSeparators.
- Several components that work together (continued):
- MenuItem -- a selection within a menu. It can dispatch an ActionEvent like a Button.
- CheckboxMenuItem -- a selection within a menu that has a checkbox in it. The state of the checkbox can be changed.
- MenuSeparator -- a nice little line that helps visually separate other items in the menu.
- Components can be laid out at specific x,y locations.
someComponent.reshape(x,y,width,height);
add(b);
- But, this is inflexible--what happens when the window is resized?
- Here, component layout is not a function of window shape or size.
- What's a LayoutManager?
- An object that implements interface LayoutManager.
- Lays out components according to some scheme.
- Associated with a particular Container via setLayout:
- FlowLayout -- arranges components left-to-right, line by line.
- BorderLayout -- arranges components at North, South, East, West and Center, like a compass
- CardLayout -- arranges components on top of one another -- only one is visible at a time
- GridLayout -- arranges components in a grid -- each gets the same amount of space
- GridBagLayout -- arranges components in a grid, but each can take up different amount of space
- Write your own!
- Take a look at BorderLayout and FlowLayout (source code is provided in the JDK)
- Very simple to write
- Caution -- make sure you respect the container's insets!
- Arranges components left-to-right until no more objects fit on that "line." Rest are
wrapped to next line.
- Default for applets.
- Manages "North", "South", "East", "West", "Center" positions.
- Elements are stretched.
- Displays one "card" at a time.
- Each component fills entire container.
- Cards are accessed via string key.
- Card brought to front via show("key")
- Arrange components into rows/columns.
- Number of rows/columns specified at construction.
- But, can leave one dimension 0, indicating "any number".
- Most flexible
- Most complex
- Will not be used in this class
- Draw a picture of what you want the interface to look like
- Two approaches to determining how to layout components:
- GridBag approach
- Figure out where everything is and assign constraints to each component
- Nesting approach
- Partition the interface into workable parts
- Lather, rinse, repeat until you're down to simple interfaces.
- For this class, use the nesting approach.
- Each component has a preferred width and height, and a minimum width and height.
- Layout managers can ignore or honor these parameters.
- The standard layout managers do the following:
- BorderLayout
North/South Components -- preferred height is honored East/West Components -- preferred width is honored
- FlowLayout
Preferred width and height is honored
- GridLayout and CardLayout
Preferred size ignored
- Use when you want
- only one of a set of components visible at a time
- to cycle through a set of components
- to create a "tabbed panel" or "notebook"
- Use when ou have one central component that should expand or shrink.
- Use when you have zero or more components that should border it with a fixed width or height.
- Order of layout:
- North & South components (expands to full width, uses preferred height)
- East & West components (expands to available height width, uses preferred width)
- Center component (expands to fill remaining space
- Useful for adjoining a label to another component
- Put the label "West", other component "Center"
- Use when you want:
- components to keep their preferred size
- components arranged so as many are visible as possible
- a horizontally-preferred ordering
- Be careful not to directly nest FlowLayouts -- they tend to not lay out terribly well due to calculation of a FlowLayout's preferred size.
- Use when you want
- All components to take up the same amount of space
- Components to be arranged in a two-dimensional grid
- A fixed number of rows or columns (the other dimension can vary)
- Use when
- components need "special" spacing relative to one another
- a simpler layout manager cannot handle the situation
- you cannot create the same effect by simple nesting of layouts
- Requires some study to really understand it
- Not difficult to write
- Good idea if the layout is likely to be needed in many cases
- Good idea if a correct layout
- would require deep nesting of other layouts
- would require a complex GridBagLayout
- has a simple physical model not handled by another layout manager
- Look at the source for BorderLayout
|