• 沒有找到結果。

Position, Size, and Alignment

在文檔中 Java™ Swing, 2nd Edition (頁 96-99)

James Elliott

Device 0: X11GraphicsDevice[screen=0]

3.5 The JComponent Class

3.5.6 Position, Size, and Alignment

JComponent

can optimize its repainting time if none of its children overlap; this is because the repaint manager does not have to compute the hidden and visible areas for each child component before rendering them. Some containers, such as

JSplitPane

, are designed so that overlap between child components is impossible, so this optimization works nicely. Other containers, such as

JLayeredPane

, have support for child components that can overlap.

JComponent

contains a property that Swing frequently calls upon to see if it can optimize component drawing:

optimizedDrawingEnabled

. In

JComponent

, this property is set to

true

by default. If overlap occurs in a subclass of

JComponent

, the subclass should override the

isOptimizedDrawingEnabled( )

accessor and return

false

. This prevents the repaint manager from using the optimized drawing process when rendering the container's children.

JComponent

contains a boolean read-only property (

paintingTile

) that indicates whether the component is currently in the process of painting a tile , which is a child component that does not overlap any other children. The

isPaintingTile( )

method returns

true

until all tiles have been painted.

The

visibleRect

property is a

Rectangle

that indicates the intersection of the component's visible rectangles with the visible rectangles of all of its ancestors. Why the intersection? Remember that you can have a contained object that is clipped by its parent. For example, you can move an internal frame so that a portion of it falls outside the parent window's clipping region. Therefore, the visible portion (the portion that is actually drawn to the screen) consists only of the intersection of the parent's visible portion and the child's visible portion. You typically do not need to access this property.

The

validateRoot

property is

false

by default. If it is set to

true

, it designates this component as the root component in a validation tree. Recall that each time a component in a container is invalidated, its container is invalidated as well, along with all of its children. This causes an invalidation to move all the way up the component hierarchy, stopping only when it reaches a component for which

isValidateRoot( )

returns

true

. Currently, the only components that set this property to

true

are

JRootPane

(which is used by all the Swing top-level components),

JScrollPane

, and

JTextField

.

The

topLevelAncestor

property contains a reference to the top-level window that contains this component, usually a

JWindow

or

JApplet

. The

rootPane

property contains the low-level

JRootPane

for this component;

JRootPane

is covered in more detail in Chapter 8.

Finally,

JComponent

contains a property called

autoscrolls

, which indicates whether a component is capable of supporting autoscrolling. This property is

false

by default. If the property is

true

, an

Autoscroller

object has been set over this component. The

Autoscroller

object monitors mouse events on the target component. If the mouse is dragged outside the component, the autoscroller forces the target component to scroll itself. Autoscrolling is typically used in containers such as

JViewport

.

You can set and retrieve a Swing component's current position and size on the screen through the

bounds

property, or more precisely, through the

location

and

size

properties of

JComponent

. The

location

property is defined as a

Point

in the parent's coordinate space where the upper-left corner of the component's bounding box resides. The

size

property is a

Dimension

that specifies the current width and height of the component. The

bounds

property is a

Rectangle

object that gives the same information: it bundles both the

location

and the

size

properties.

Figure 3-6 shows how Swing measures the size and location of a component.

Figure 3-6. Working with the bounds, size, and location properties

Unlike the AWT

Component

class, the

getBounds( )

accessor in

JComponent

can take a preinstantiated

Rectangle

object:

Rectangle myRect = new Rectangle( );

myRect = component.getBounds(myRect);

If a

Rectangle

is supplied, the

getBounds( )

method alters each of the fields in the passed-in

Rectangle

to reflect the component's current size and position, returning a copy of it. If the reference passed in is a

null

, the method instantiates a new

Rectangle

object, sets its values, and returns it. You can use the former approach to reduce the number of garbage rectangles created and discarded over multiple calls to

getBounds( )

, which increases the efficiency of your application.

The

setBounds( )

method alters the component's size and position. This method also takes a

Rectangle

object. If the new settings are different from the previous settings, the component is moved, typically resized, and invalidated. If the component has a parent, it is invalidated as well. Be warned that various layout managers may override any changes you attempt to make to the

bounds

property. Invalidating a component with a call to

setBounds( )

may force the layout manager to recompute and reset the bounds of the component in relation to the other components, resolving it to the same size as before.

Here is a short example that shows how to retrieve the current position and size of any Swing component:

JFrame frame = new JFrame("Test Frame");

frame.setBounds(20,20,200,200);

frame.setVisible(true);

Rectangle r = new Rectangle( );

r = frame.getBounds(r);

System.out.println("X = " + r.x( ));

System.out.println("Y = " + r.y( ));

System.out.println("Width = " + r.width( ));

System.out.println("Height = " + r.height( ));

There is a shorthand approach for retrieving each of the bounds properties.

JComponent

contains four methods that directly access them:

getX( )

,

getY( )

,

getWidth( )

, and

getHeight( )

. You can use these accessors directly instead of instantiating a

Rectangle

object on the heap with a call to

getBounds( )

. Consequently, you can replace the last six lines with the following four:

System.out.println("X = " + frame.getX( ));

System.out.println("Y = " + frame.getY( ));

System.out.println("Width = " + frame.getWidth( ));

System.out.println("Height = " + frame.getHeight( ));

In addition, if it is just the size or location you are concerned with, you can use the

getSize( )

and

getLocation( )

accessors to set or retrieve the size or location. Size is specified as a

Dimension

while location is given as a

Point

. Like

getBounds( )

, the

getLocation( )

accessor also allows the programmer to pass in a preinstantiated

Point

object. If one is passed in, the method alters the coordinates of the

Point

instead of instantiating a new object.

Point myPoint = new Point( );

myPoint = component.getLocation(myPoint);

You can still use the

setSize( )

and

setLocation( )

methods of

java.awt.Component

if you prefer to code with those as well. Again, note that when altering the size of the component, the layout manager may override the new value and reset it to its previous value, thus ignoring your new size values.

The three well-known AWT sizing properties,

minimumSize

,

preferredSize

, and

maximumSize

, are accessible through

JComponent

.

minimumSize

indicates the smallest size for the component when it is in a container.

preferredSize

contains the size at which the container's layout manager should strive to draw the component.

maximumSize

indicates the largest size the component should be when displayed in a container. If none of these properties are set by the user, they are always calculated by the component's UI delegate or directly by the layout manager of the container, in that order. The methods

setMinimumSize( )

,

setPreferredSize

, and

setMaximumSize( )

allow you to change these properties without subclassing.

Finally,

JComponent

contains two read/write properties that help interested layout managers align the component in a container:

alignmentX

and

alignmentY

. Both of these properties contain floating-point values between 0.0 and 1.0; the numbers determine the position of the component relative to any siblings. A number closer to 0 indicates that the component should be positioned closer to the left or top side, respectively. A perfect 0.5 indicates that the component should be placed at the center, while a number nearing 1 indicates that the component should be positioned closer to the right or bottom. Currently, the only layout managers that use these properties are the

BoxLayout

and

OverlayLayout

managers; all AWT 1.1 layout managers ignore these properties and position their children by other means. We discuss these managers further in Chapter 11.

在文檔中 Java™ Swing, 2nd Edition (頁 96-99)

相關文件