Java Reference
In-Depth Information
Containers and components . A container is an instance of a class that can con-
tain components (like buttons and text fields). A component is an instance of a
class that can be placed in a container. Panel , JPanel , and Box are examples of
components that are themselves containers. Using these three, one can nest com-
ponents to any level.
Basic components . The basic components are labels, buttons, text fields, text
areas, checkboxes, radio buttons, combo boxes, lists, and sliders.
Layout managers . A layout manager for a container is an instance of a class
that takes care of “laying out” the components that have been added to the con-
tainer. Each container has a layout manager associated with it. Each different
kind of layout manager uses different rules for laying out the components. The
primary layout-manager classes are BorderLayout , FlowLayout , and
BoxLayout , but there are others.
Graphics . An instance of container class JPanel has a method paint , which
can be used to draw on the panel using methods of class Graphics .
Listening to a GUI . In order to listen to an event like a mouse click, a click of
a button, or the press of the enter/return key in a text field, an instance of a class
that implements ActionListener has to be registered with the component on
which the event takes place. The instance of the class needs a method action-
Performed , which processes the event.
Dialog windows . Class JOptionPane has several methods that make it easy to
display a modal window on the monitor, wait for the user to close it, and then
retrieve the user's response.
Lightweight versus heavyweight . Top-level windows are heavyweight —they
have associated “peers”, written in machine language, that do all the window
drawing. Components can be heavyweight or lightweight , which means that they
do not have such peers. All the awt components are heavyweight, but the swing
components are lightweight. Mixing lightweight and heavyweight components
may lead to inconsistencies and unwanted behavior in a window. So that this
does not happen, try to use all swing components, or all awt components.
Exercises for Chapter 17
E1. Write (and test) a subclass of JFrame that has two components:
• in the west, a JLabel that contains “waist size:”;
• in the east, a JTextField with initial value “34 inches”.
E2. Same as exercise E1, but right-adjust the label —see Sec. 17.2.2. Drag the
window to make it wider to see what happens to the label.
E3. Like exercise E1, but make the text field uneditable.
Search WWH ::

Custom Search