欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

alignmentlayout.java

PIY(Program It Yourself)是一个基于Java的应用程序开发环境
JAVA
字号:
package piy;

import java.awt.*;
import java.util.ArrayList;
import java.io.Serializable;

/**
* Every container is given this layout manager by PIY.  This gives every component added
* to a container an alignment property, which can be one of LEFT, RIGHT, TOP, BOTTOM, CLIENT,
* or NONE from the constants in the Align class. It is similar in operation to BorderLayout, 
* but allows absolute positioning of components (ie. alignment NONE). The operation of this 
* layout manager is undefined when there is more than one element with the client layout 
* specified for it.
*
* @author David Vivash
* @version 1.0, 25/02/01
*/
public class AlignmentLayout implements LayoutManager, Serializable 
{
	private ArrayList leftAligned	= new ArrayList(1);
	private ArrayList rightAligned	= new ArrayList(1);
	private ArrayList topAligned	= new ArrayList(1);
	private ArrayList bottomAligned	= new ArrayList(1);
	private ArrayList clientAligned	= new ArrayList(1);
	private ArrayList notAligned	= new ArrayList(4);
		
	/**
	* Adds the specified component with the specified name to the layout. It is given the
	* default alignment of NONE. The name of the component is ignored (who cares about it?)
	* @param name the name of the component (NOT USED)
	* @param comp the component to add
	*/
	public void addLayoutComponent(String name, Component comp) {
		notAligned.add(comp);
	}
	
	/**
	* Lays out the container in the specified panel.
	* @param parent the container's parent
	*/
	public void layoutContainer(Container parent) {

		int topMargin		= 0;
		int bottomMargin	= 0;
		int leftMargin		= 0;
		int rightMargin		= 0;

		Dimension pSize = parent.getSize();

		Component element = null;

		for (int i=0; i<topAligned.size(); i++) {
			element = (Component)topAligned.get(i);
			element.setBounds(0, topMargin, pSize.width, element.getSize().height);
			if (element instanceof Container)
				((Container)element).doLayout();

			topMargin += element.getSize().height;
		}

		for (int i=0; i<bottomAligned.size(); i++) {
			element = (Component)bottomAligned.get(i);
			element.setBounds(0, pSize.height - element.getSize().height - bottomMargin, pSize.width, element.getSize().height);
			if (element instanceof Container)
				((Container)element).doLayout();

			bottomMargin += element.getSize().height;
		}

		for (int i=0; i<leftAligned.size(); i++) {
			element = (Component)leftAligned.get(i);
			element.setBounds(leftMargin, topMargin, element.getSize().width, pSize.height - topMargin - bottomMargin);
			if (element instanceof Container)
				((Container)element).doLayout();

			leftMargin += element.getSize().width;
		}

		for (int i=0; i<rightAligned.size(); i++) {
			element = (Component)rightAligned.get(i);
			element.setBounds(pSize.width - rightMargin - element.getSize().width, topMargin, element.getSize().width, pSize.height - topMargin - bottomMargin);
			if (element instanceof Container)
				((Container)element).doLayout();

			rightMargin += element.getSize().width;
		}

		for (int i=0; i<clientAligned.size(); i++) {
			element = (Component)clientAligned.get(i);
			element.setBounds(leftMargin, topMargin, pSize.width - leftMargin - rightMargin, pSize.height - topMargin - bottomMargin);
			if (element instanceof Container)
				((Container)element).doLayout();
		}


	}

	/**
	* Calculates the minimum size dimensions for the specified panel given the components 
	* in the specified parent container. THIS IS NOT USED.
	* @param parent the container's parent
	* @return the size requirements of the container 
	*/
	public Dimension minimumLayoutSize(Container parent) {
		return new Dimension(0,0);
	}
	
	/**
	* Calculates the preferred size dimensions for the specified panel given the components 
	* in the specified parent container. THIS IS NOT USED.
	* @param parent the component's parent
	* @return the size requirements of the container
	*/
	public Dimension preferredLayoutSize(Container parent) {
		return new Dimension(0,0);
	}
	
	/**
	* Removes the specified component from the layout. 
	* @param comp the component to remove
	*/
	public void removeLayoutComponent(Component comp) {
		int pos = -1;

			 if ( (pos = notAligned.indexOf(comp)) >= 0) 	notAligned.remove(pos);
		else if ( (pos = topAligned.indexOf(comp)) >= 0) 	topAligned.remove(pos);
		else if ( (pos = bottomAligned.indexOf(comp)) >= 0)	bottomAligned.remove(pos);
		else if ( (pos = leftAligned.indexOf(comp)) >= 0)	leftAligned.remove(pos);
		else if ( (pos = rightAligned.indexOf(comp)) >= 0)	rightAligned.remove(pos);
		else if ( (pos = clientAligned.indexOf(comp)) >= 0)	clientAligned.remove(pos);

	}

	/**
	* Sets the alignemnt of the particular component. The component must be exist in this
	* layout manager's context, and the alignment must be one of LEFT, RIGHT, TOP, BOTTOM, CLIENT.
	* If the specified component is not found in this layout manager's context, it is added
	* as if <code>addLayoutComponent(null, comp)</code> has been called.  If the alignment
	* string is not recognised, a default alignment of NONE is used.
	* @param comp the component whose alignment should be changed
	* @param alignment the new alignment for the component
	*/
	public void setAlignment(Component comp, String alignment) {
		removeLayoutComponent(comp);

		if (alignment == null) alignment = Align.NONE;

		if (alignment.equals(Align.LEFT))
			leftAligned.add(comp);
		else if (alignment.equals(Align.RIGHT))
			rightAligned.add(comp);
		else if (alignment.equals(Align.TOP))
			topAligned.add(comp);
		else if (alignment.equals(Align.BOTTOM))
			bottomAligned.add(comp);
		else if (alignment.equals(Align.CLIENT))
			clientAligned.add(comp);
		else
			notAligned.add(comp);
	}

	/**
	* Gets the alignment of the particular component. The component must be exist in this
	* layout manager's context, and the alignment returned will be one of LEFT, RIGHT, TOP, 
	* BOTTOM, CLIENT.
	* If the specified component is not found in this layout manager's context, the alignment
	* string of NONE is used.
	* @param comp the component whose alignment should be changed
	* @return the alignment string of the component
	*/
	public String getAlignment(Component comp) {
		int pos = -1;

			 if ( (pos = notAligned.indexOf(comp)) >= 0) 	return Align.NONE;
		else if ( (pos = topAligned.indexOf(comp)) >= 0) 	return Align.TOP;
		else if ( (pos = bottomAligned.indexOf(comp)) >= 0)	return Align.BOTTOM;
		else if ( (pos = leftAligned.indexOf(comp)) >= 0)	return Align.LEFT;
		else if ( (pos = rightAligned.indexOf(comp)) >= 0)	return Align.RIGHT;
		else if ( (pos = clientAligned.indexOf(comp)) >= 0)	return Align.CLIENT;

		return Align.NONE;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -