Sunday, February 12, 2012

Setting size hints

To ensure that components are positioned well, you may require certain components to adhere to maximum or minimum specifications.

You can use size hints to specify a number of preferred maximum and minimum component sizes.
The layout manager implements the specified sizes for container components. However, only BoxLayout and SpringLayout implement the requested maximum size for components.
To set size hints, you use the component's setMinimumSize, setPreferredSize, and setMaximumSize methods.

For example, you can use the setMaximumSize method to set the size hint for the component to 150 by 300 pixels.

Alternatively, you can override the appropriate getter methods of a component using a subclass. The component getter methods are getMinimumSize, getPreferredSize, and getMaximumSize.
component.setMaximumSize(new Dimension(150, 300));

You can also provide alignment hints for components, although these are only implemented by the BoxLayout layout manager.
You set alignment hints using the component's setAlignmentX and setAlignmentY methods. For example, you can specify that a button is aligned 40 pixels from the top.

As with size hints, you can also override the component's getAlignmentX and getAlignmentY methods by creating a subclass.

myButton.setAlignmentX( (float) 40 ) ;

0 comments:

Post a Comment