基于来自 Stanislav Lapitsky 的这段代码 ( http://java-sl.com/tip_multiple_floatable_toolbars.html ),这是一个新布局的工作实现,其行为类似于 BorderLayout,但您可以在每个位置添加多个子级,包括工具栏:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
* Extends BorderLayout with multiple components in the northList, southList,
* eastList, westList and centerList. Layout is used for correct working
* multiple toolbars.
*
* @author Stanislav Lapitsky
* @version 1.0
*/
public class MultiBorderLayout implements LayoutManager2, java.io.Serializable {
private int vgap;
private int hgap;
/**
* list of the northList region components
*/
private Vector<Component> northList = new Vector<>();
/**
* list of the southList region components
*/
private Vector<Component> southList = new Vector<>();
/**
* list of the westList region components
*/
private Vector<Component> westList = new Vector<>();
/**
* list of the eastList region components
*/
private Vector<Component> eastList = new Vector<>();
/**
* list of the centerList region components
*/
private Vector<Component> centerList = new Vector<>();
/**
* Constructs default layout instance.
*/
public MultiBorderLayout() {
this( 0, 0 );
}
/**
* Constructs new layout instance with defined parameters.
*
* @param hgap the horizontal gap.
* @param vgap the vertical gap.
*/
public MultiBorderLayout( int hgap, int vgap ) {
this.hgap = hgap;
this.vgap = vgap;
}
/**
* Returns the horizontal gap between components.
*
* @return the horizontal gap between components
*/
public int getHgap() {
return hgap;
}
/**
* Sets the horizontal gap between components.
*
* @param hgap the horizontal gap between components
*/
public void setHgap( int hgap ) {
this.hgap = hgap;
}
/**
* Returns the vertical gap between components.
*
* @return the vertical gap between components
*/
public int getVgap() {
return vgap;
}
/**
* Sets the vertical gap between components.
*
* @param vgap the vertical gap between components
*/
public void setVgap( int vgap ) {
this.vgap = vgap;
}
private List<Component> getComponentList( Object constraint ) {
if ( BorderLayout.CENTER.equals( constraint ) ) {
return centerList;
}
if ( BorderLayout.NORTH.equals( constraint ) ) {
return northList;
}
if ( BorderLayout.SOUTH.equals( constraint ) ) {
return southList;
}
if ( BorderLayout.EAST.equals( constraint ) ) {
return eastList;
}
if ( BorderLayout.WEST.equals( constraint ) ) {
return westList;
}
throw new IllegalArgumentException( "Unknown constraint: " + constraint );
}
/**
* Removes the specified component from this border layout. This method is
* called when a container calls its <code>remove</code> or
* <code>removeAll</code> methods. Most applications do not call this method
* directly.
*
* @param comp the component to be removed.
*/
public void removeLayoutComponent( Component comp ) {
synchronized ( comp.getTreeLock() ) {
southList.remove( comp );
northList.remove( comp );
centerList.remove( comp );
westList.remove( comp );
eastList.remove( comp );
}
}
/**
* Determines the minimum size of the <code>target</code> container using this
* layout manager.
* <p>
*
* This method is called when a container calls its <code>getMinimumSize</code>
* method. Most applications do not call this method directly.
*
* @param target the container in which to do the layout.
* @return the minimum dimensions needed to lay out the subcomponents of the
* specified container.
*/
public Dimension minimumLayoutSize( Container target ) {
synchronized ( target.getTreeLock() ) {
Dimension dim = new Dimension( 0, 0 );
Component c;
if ( eastList.size() > 0 ) {
for ( int i = 0; i < eastList.size(); i++ ) {
c = (Component) eastList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getMinimumSize();
dim.width += d.width + this.getHgap();
dim.height = Math.max( d.height, dim.height );
}
}
if ( westList.size() > 0 ) {
for ( int i = 0; i < westList.size(); i++ ) {
c = (Component) westList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getMinimumSize();
dim.width += d.width + this.getHgap();
dim.height = Math.max( d.height, dim.height );
}
}
if ( centerList.size() > 0 ) {
for ( int i = 0; i < centerList.size(); i++ ) {
c = (Component) centerList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getMinimumSize();
dim.width += d.width;
dim.height = Math.max( d.height, dim.height );
}
}
if ( northList.size() > 0 ) {
for ( int i = 0; i < northList.size(); i++ ) {
c = (Component) northList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getMinimumSize();
dim.width = Math.max( d.width, dim.width );
dim.height += d.height + this.getVgap();
}
}
if ( southList.size() > 0 ) {
for ( int i = 0; i < southList.size(); i++ ) {
c = (Component) southList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getMinimumSize();
dim.width = Math.max( d.width, dim.width );
dim.height += d.height + this.getVgap();
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right;
dim.height += insets.top + insets.bottom;
return dim;
}
}
/**
* Determines the preferred size of the <code>target</code> container using this
* layout manager, based on the components in the container.
* <p>
*
* Most applications do not call this method directly. This method is called
* when a container calls its <code>getPreferredSize</code> method.
*
* @param target the container in which to do the layout.
* @return the preferred dimensions to lay out the subcomponents of the
* specified container.
*/
@Override
public Dimension preferredLayoutSize( Container target ) {
synchronized ( target.getTreeLock() ) {
Dimension dim = new Dimension( 0, 0 );
Component c;
if ( eastList.size() > 0 ) {
for ( int i = 0; i < eastList.size(); i++ ) {
c = (Component) eastList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
dim.width += d.width + this.getHgap();
dim.height = Math.max( d.height, dim.height );
}
}
if ( westList.size() > 0 ) {
for ( int i = 0; i < westList.size(); i++ ) {
c = (Component) westList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
dim.width += d.width + this.getHgap();
dim.height = Math.max( d.height, dim.height );
}
}
if ( centerList.size() > 0 ) {
for ( int i = 0; i < centerList.size(); i++ ) {
c = (Component) centerList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
dim.width += d.width;
dim.height = Math.max( d.height, dim.height );
}
}
if ( northList.size() > 0 ) {
for ( int i = 0; i < northList.size(); i++ ) {
c = (Component) northList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
dim.width = Math.max( d.width, dim.width );
dim.height += d.height + this.getVgap();
}
}
if ( southList.size() > 0 ) {
for ( int i = 0; i < southList.size(); i++ ) {
c = (Component) southList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
dim.width = Math.max( d.width, dim.width );
dim.height += d.height + this.getVgap();
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right;
dim.height += insets.top + insets.bottom;
return dim;
}
}
/**
* Lays out the container argument using this border layout.
* <p>
*
* This method actually reshapes the components in the specified container in
* order to satisfy the constraints of this <code>BorderLayout</code> object.
* The <code>NORTH</code> and <code>SOUTH</code> components, if any, are placed
* at the top and bottom of the container, respectively. The <code>WEST</code>
* and <code>EAST</code> components are then placed on the left and right,
* respectively. Finally, the <code>CENTER</code> object is placed in any
* remaining space in the middle.
* <p>
*
* Most applications do not call this method directly. This method is called
* when a container calls its <code>doLayout</code> method.
*
* @param target the container in which to do the layout.
*/
public void layoutContainer( Container target ) {
synchronized ( target.getTreeLock() ) {
Insets insets = target.getInsets();
int top = insets.top;
int bottom = target.getHeight() - insets.bottom;
int left = insets.left;
int right = target.getWidth() - insets.right;
Component c;
if ( northList.size() > 0 ) {
for ( int i = 0; i < northList.size(); i++ ) {
c = (Component) northList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
c.setSize( right - left, d.height );
c.setBounds( left, top, right - left, c.getHeight() );
top += d.height;
}
}
if ( southList.size() > 0 ) {
for ( int i = 0; i < southList.size(); i++ ) {
c = (Component) southList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
c.setSize( right - left, d.height );
c.setBounds( left, bottom - d.height, right - left, c.getHeight() );
bottom -= d.height;
}
}
if ( eastList.size() > 0 ) {
for ( int i = 0; i < eastList.size(); i++ ) {
c = (Component) eastList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
c.setSize( d.width, bottom - top );
c.setBounds( right - d.width, top, c.getWidth(), bottom - top );
right -= d.width;
}
}
if ( westList.size() > 0 ) {
for ( int i = 0; i < westList.size(); i++ ) {
c = (Component) westList.get( i );
if ( !c.isVisible() ) {
continue;
}
Dimension d = c.getPreferredSize();
c.setSize( d.width, bottom - top );
c.setBounds( left, top, c.getWidth(), bottom - top );
left += d.width;
}
}
if ( centerList.size() > 0 ) {
for ( int i = 0; i < centerList.size(); i++ ) {
c = (Component) centerList.get( i );
if ( !c.isVisible() ) {
continue;
}
c.setBounds( left, top, right - left, bottom - top );
}
}
}
}
public List<Component> getLayoutComponents( Object constraints ) {
if ( ( constraints == null ) || ( constraints instanceof String ) ) {
// Return copy, don't expose internal Vectors
return new ArrayList<>( getComponentList( constraints == null ? BorderLayout.CENTER : (String) constraints ) );
}
throw new IllegalArgumentException( "Cannot get layout components: constraint must be a string (or null)" );
}
/**
* Adds the specified component to the layout, using the specified constraint
* object. For border layouts, the constraint must be one of the following
* constants: <code>NORTH</code>, <code>SOUTH</code>, <code>EAST</code> ,
* <code>WEST</code>, or <code>CENTER</code>.
* <p>
*
* Most applications do not call this method directly. This method is called
* when a component is added to a container using the <code>Container.add</code>
* method with the same argument types.
*
* @param name The feature to be added to the LayoutComponent attribute.
* @param comp the component to be added.
*/
@Override
public void addLayoutComponent( Component comp, Object constraints ) {
synchronized ( comp.getTreeLock() ) {
if ( ( constraints == null ) || ( constraints instanceof String ) ) {
addLayoutComponent( (String) constraints, comp );
} else {
throw new IllegalArgumentException( "cannot add to layout: constraint must be a string (or null)" );
}
}
}
/**
* Returns the maximum dimensions for this layout given the components in the
* specified target container.
*
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
* @see #preferredLayoutSize
*/
@Override
public Dimension maximumLayoutSize( Container target ) {
return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
}
/**
* Returns the alignment along the x axis. This specifies how the component
* would like to be aligned relative to other components. The value should be a
* number between 0 and 1 where 0 represents alignment along the origin, 1 is
* aligned the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentX( Container parent ) {
return 0.5f;
}
/**
* Returns the alignment along the y axis. This specifies how the component
* would like to be aligned relative to other components. The value should be a
* number between 0 and 1 where 0 represents alignment along the origin, 1 is
* aligned the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentY( Container parent ) {
return 0.5f;
}
@Override
public void invalidateLayout( Container target ) {
}
@Override
public void addLayoutComponent( String name, Component comp ) {
synchronized ( comp.getTreeLock() ) {
/*
* Special case: treat null the same as "Center".
*/
if ( name == null ) {
name = "Center";
}
getComponentList( name ).add( comp );
}
}
}
原始代码在方法名称中有错字preferredLayoutSize
,它还扩展了 BorderLayout 继承了一些方法(即public Component getLayoutComponent(Object constraints)
)或对这种情况没有意义的假设。