Set this layout on your container and do:
container.add(comp1, new PanelBounds(0.0, 0.0, 0.333, 1.0));
container.add(comp2, new PanelBounds(0.333, 0.0, 0.666, 1.0));
/**
* Layout manager for components which wish to be sized as a fraction of the size
* of their parent container.
*
* Containers using this layout must have components added using the
* <code>add(Component, Object)</code> method where the <code>Object</code>
* argument must be a <code>PanelBounds</code> object.
* The <code>PanelBounds</code> argument represents a rectangle of area covered
* by the <code>Component</code> to be added. The size of the rectangle should
* represent a fraction of the size of the parent <code>Container</code> where
* a rectangle of width = 1.0 and height = 1.0 would be the same size as the
* parent.
* E.g. If you wished to add a button to the whole of the bottom half of a container,
* you would do this: container.add(button, new PanelBounds(0.0, 0.5, 1.0, 0.5));
*
*
*
*
*
* @author Jim Morrison
* @version $Revision: 1.2 $
*/
public class FractionalLayout implements LayoutManager2
{
private Map components = new HashMap();
/**
* Creates a new <code>FractionalLayout</code>.
*/
public FractionalLayout()
{
// empty constructor
}
/**
* Adds the specified component to the layout, using the specified
* constraint object.
* @param comp the component to be added
* @param constraints where/how the component is added to the layout.
*/
public void addLayoutComponent( Component comp, Object constraints )
{
synchronized(comp.getTreeLock())
{
if( constraints == null || !( constraints instanceof PanelBounds ) )
{
throw new IllegalArgumentException("cannot add to layout: constraint must be a PanelBounds"); //$NON-NLS-1$
}
components.put( constraints, comp );
}
}
/**
* @see java.awt.LayoutManager2#maximumLayoutSize(java.awt.Container)
*/
public Dimension maximumLayoutSize( Container target )
{
return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
}
/**
* @see java.awt.LayoutManager2#getLayoutAlignmentX(java.awt.Container)
*/
public float getLayoutAlignmentX( Container target )
{
return 0.5f;
}
/**
* @see java.awt.LayoutManager2#getLayoutAlignmentY(java.awt.Container)
*/
public float getLayoutAlignmentY( Container target )
{
return 0.5f;
}
/**
* @see java.awt.LayoutManager2#invalidateLayout(java.awt.Container)
*/
public void invalidateLayout( Container target )
{
// do nothing
}
/**
* This does nothing since constraints are required.
*
* @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
*/
public void addLayoutComponent( String name, Component comp )
{
// do nothing
}
/**
* Removes the specified component from the layout.
* @param comp the component to be removed
*/
public void removeLayoutComponent( Component comp )
{
components.remove( comp );
}
/**
* @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
*/
public Dimension preferredLayoutSize( Container parent )
{
return parent.getPreferredSize();
}
/**
* @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
*/
public Dimension minimumLayoutSize( Container parent )
{
return new Dimension(50, 50);
}
/**
* Lays out the specified container.
* @param parent the component which needs to be laid out
*/
public void layoutContainer( Container parent )
{
synchronized( parent.getTreeLock() )
{
Insets insets = parent.getInsets();
if( components.size() == 0 )
{
return;
}
Dimension size = parent.getSize();
int totalW = size.width - ( insets.left + insets.right );
int totalH = size.height - ( insets.top + insets.bottom );
Iterator keys = components.keySet().iterator();
while( keys.hasNext() )
{
PanelBounds bounds = (PanelBounds) keys.next();
Component comp = (Component) components.get( bounds );
if( bounds != null )
{
int x = (int) ( insets.left + ( totalW * bounds.x ) );
int y = (int) ( insets.top + ( totalH * bounds.y ) );
int width = (int) ( insets.left + ( totalW * bounds.width ) );
int height = (int) ( insets.top + ( totalH * bounds.height ) );
comp.setBounds( x, y, width, height );
}
else
{
comp.setSize( 0, 0 );
}
}
}
}
}
public class PanelBounds extends Rectangle2D.Double
{
/**
* Constructs a new PanelBounds, initialized to location (0,?) and size (0,?).
*/
public PanelBounds()
{
}
/**
* Constructs and initializes a PanelBounds object from the specified double coordinates.
* @param x,爕 the coordinates of the upper left corner
* of the newly constructed PanelBounds
* @param width the width of the newly constructed PanelBounds
* @param height the height of the newly constructed PanelBounds
*/
public PanelBounds(double x, double y, double width, double height)
{
super(x, y, width, height);
}
/**
* Sets the x position of the upper left corner.
* @param x The x to set.
*/
public void setX(double x)
{
this.x = x;
}
/**
* Sets the y position of the upper left corner.
* @param y The y to set.
*/
public void setY(double y)
{
this.y = y;
}
/**
* Sets the width.
* @param width The width to set.
*/
public void setWidth(double width)
{
this.width = width;
}
/**
* Sets the height.
* @param height The height to set.
*/
public void setHeight(double height)
{
this.height = height;
}
//public String toString()
//{
//return
//}
}
Cheers,
Jim