Java Reference
In-Depth Information
Example 10•29: AppletMenuBar.java (continued)
* These methods tell the container how big the menubar wants to be.
*
**/
public Dimension getMinimumSize() { return getPreferredSize(); }
public Dimension getPreferredSize() {
if (remeasure) measure();
return prefsize;
}
/** @deprecated Here for compatibility with Java 1.0 */
public Dimension minimumSize() { return getPreferredSize(); }
/** @deprecated Here for compatibility with Java 1.0 */
public Dimension preferredSize() { return getPreferredSize(); }
/**
* This method is called when the underlying AWT component is created.
* We can't measure ourselves (no font metrics) until this is called.
**/
public void addNotify() {
super.addNotify();
measure();
}
/** This method tells the container not to give us keyboard focus */
public boolean isFocusTraversable() { return false; }
}
Exercises
10-1.
Take a look again at Figure 10-7. Write a class that produces a sample
layout like
GridBagLayoutPane
, without using the
GridBagLayout
layout
manager. You'll probably want to use
BorderLayout
and the
Box
container
or
BoxLayout
layout manager.
10-2. The
ScribblePane2
class has a serious shortcoming: the scribbles are
erased whenever the window is obscured and then uncovered. Modify
ScribblePane2
so that it remembers the user's scribble. You need to mod-
ify the
lineto()
method so that in addition to drawing a line, it also stores
the coordinates and color of the line (using a
Vector
or
ArrayList
, per-
haps).
JComponent
calls its
paintComponent()
method whenever the com-
ponent needs to be redrawn. Read the documentation for this method,
then override it to redraw the scribble using the saved coordinates. Finally,
you also need to modify the
clear()
method to work with this new sys-
tem.
10-3. The
ItemChooser
class allows items to be specified only when the compo-
nent is created. Add methods that allow items to be added and removed.
Make the methods work regardless of the presentation type in use.
10-4. The
Scribble
application defines and uses a
ColorAction
class to allow
the user to select the current drawing color. Add a
LineWidthAction
class
that lets the user select the line width. To accomplish this, give the
Scrib-
blePane2
component a
setLineWidth()
method so that it can draw using