⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jstitlebarlayout.java

📁 Java自定义窗体JsFrame。简介见:http://jason0086.spaces.live.com/Blog/cns!A797D0C5C0C13C92!518.entry
💻 JAVA
字号:
package com.hfkj.jsframe.titlebar;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;

import com.hfkj.jsframe.layout.JsLayoutManager;

/**
 * A js title bar layout lays out a container, arranging the components in its own order:
 * <p>
 * <ul>
 * <li><code>ICON</code>:
 * The head in the direction of west in this layout.
 * <li><code>TITLE</code>:
 * The east neighbor to the <code>ICON</code> in this layout.
 * <li><code>CLOSE</code>:
 * The tail in the direction of east in this layout.
 * <li><code>MAXIMIZE_RESTORE</code>:
 * The east neighbor to the <code>CLOSE</code> in this layout.
 * <li><code>MINIMIZE</code>:
 * The east neighbor to the <code>MAXIMIZE_RESTORE</code> in this layout.
 * </ul>
 * <p>
 * Each of the above regions may contains no more than one component, and is identified
 * by a corresponding constraints:
 * <code>ICON</code>, <code>TITLE</code>, <code>MINIMIZE</code>,
 * <code>MAXIMIZE_RESTORE</code>, <code>CLOSE</code>.
 * When adding a component to a container with a js title bar layout, use one of these
 * five constraints, for example:
 * <pre>
 * Panel p = new Panel();
 * p.setLayout(new JsTitleBarLayout());
 * p.add(new Button("Okay"), JsTitleBarLayout.ICON);
 * </pre>
 * <p>
 * The components are laid out according to their preferred sizes and the constraints of
 * the container's size.
 * <p>
 * Here is an example of five regions in a frame laid out using the <code>JsTitleBarLayout</code>
 * layout manager:
 * <p>
 * <img src="doc-files/JsTitleBarLayout.png" alt="Diagram of a frame demonstrating JsTitleBarLayout.
 * Each section of the JsTitleBarLayout contains a Button associated with its location in the layout,
 * one of: ICON, TITLE, MINIMIZE, MAXIMIZE_RESTORE, CLOSE.">
 * <p>
 * The code for this frame is as follows:
 * <p>
 * <pre>
 * import java.awt.Button;
 * import java.awt.Frame;
 * import java.awt.Color;
 * 
 * import com.hfkj.jsframe.titlebar.JsTitleBarLayout;
 * 
 * public class Test {
 * 
 * 	public static void main(String[] args) {
 * 		Frame xFrm = new Frame("Test JsTitleBarLayout");
 * 		xFrm.setUndecorated(true);
 * 		xFrm.setSize(400, 25);
 * 		xFrm.setLocation(300, 300);
 * 		xFrm.setBackground(Color.YELLOW);
 * 		xFrm.setLayout(new JsTitleBarLayout(5));
 * 		xFrm.add(new Button("Icon"), JsTitleBarLayout.ICON);
 * 		xFrm.add(new Button("Title"), JsTitleBarLayout.TITLE);
 * 		xFrm.add(new Button("-"), JsTitleBarLayout.MINIMIZE);
 * 		xFrm.add(new Button("O"), JsTitleBarLayout.MAXIMIZE_RESTORE);
 * 		xFrm.add(new Button("X"), JsTitleBarLayout.CLOSE);
 * 		xFrm.setVisible(true);
 * 	}
 * 		
 * }
 * </pre>
 * 
 * @see JsTitleBar
 * 
 * @version 1.0 01/05/09
 * @author Jason (MSN:www.jason0086.com@hotmail.com)
 */
public class JsTitleBarLayout implements JsLayoutManager {

	/**
	 * The icon layout constraint(north west of container).
	 */
	public static final String ICON = "Icon";
	
	/**
	 * The title layout constraint(north of container).
	 */
	public static final String TITLE = "Title";
	
	/**
	 * The minimize layout constraint(center of container).
	 */
	public static final String MINIMIZE = "Minimize";
	
	/**
	 * The maximize layout constraint(west of container).
	 */
	public static final String MAXIMIZE_RESTORE = "Maximize_Restore";
	
	/**
	 * The close layout constraint(north east of container).
	 */
	public static final String CLOSE = "Close";
	
	/**
	 * Constant to specify components location to be the icon portion of the layout.
	 */
	private Component icon = null;
	
	/**
	 * Constant to specify components location to be the title portion of the layout.
	 */
	private Component title = null;
	
	/**
	 * Constant to specify components location to be the minimize portion of the layout.
	 */
	private Component minimize = null;
	
	/**
	 * Constant to specify components location to be the maximize portion of the layout.
	 */
	private Component maximize = null;
	
	/**
	 * Constant to specify components location to be the close portion of the layout.
	 */
	private Component close = null;
	
	/**
	 * The horizontal gap between components.
	 */
	private int hgap = 0;
	
	/**
	 * Constructs a new layout with no gaps between components.
	 *
	 */
	public JsTitleBarLayout() {
		this(0);
	}
	
	/**
	 * Constructs a layout with the specify gaps between components.
	 * @param h the horizontal gap between components
	 */
	public JsTitleBarLayout(int h) {
		hgap = h;
	}
	
	/**
	 * Sets the horizontal gap between components.
	 * @param h the horizontal gap
	 */
	public void setHgap(int h) {
		hgap = h;
	}
	
	/**
	 * Returns the horizontal gap between components.
	 * @return the horizontal gap
	 */
	public int getHgap() {
		return hgap;
	}
	
	/**
	 * Returns the component that was added using the given constraint.
	 * @param constraints the location of the component in this layout
	 * @return the component associated with the given constraints.
	 */
	public Component getLayoutComponent(Object constraints) {
		if (ICON.equals(constraints)) {
			return icon;
		}
		else if (TITLE.equals(constraints)) {
			return title;
		}
		else if (MINIMIZE.equals(constraints)) {
			return minimize;
		}
		else if (MAXIMIZE_RESTORE.equals(constraints)) {
			return maximize;
		}
		else if (CLOSE.equals(constraints)) {
			return close;
		}
		else {
			throw new IllegalArgumentException("Can not get component: unknown constraint:" + constraints);
		}
	}

	/**
	 * Returns the aligment along the x axis.
	 * This specifies how the component would like to 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.
	 * @param target the container
	 * @return the aligment along the x axis
	 */
	public float getLayoutAlignmentX(Container target) {
		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.
	 * @param target the container
	 * @return the aligment along the y axis
	 */
	public float getLayoutAlignmentY(Container target) {
		return 0.5f;
	}

	/**
	 * Invalidates the layout, 
	 * indicating that if the layout manager has cached information it should be discarded.
	 * @param target the container
	 */
	public void invalidateLayout(Container target) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Determines the maximum dimensions of the target container using this layout manager.
	 * @param target the container
	 * @return the maximum dimensions for this layout
	 */
	public Dimension maximumLayoutSize(Container target) {
		return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
	}
	
	/**
	 * Adds the specify component to the layout, using the specify constraint object.
	 * The constraint should be null or one of the following constants:
	 * <code>ICON</code>, <code>TITLE</code>, <code>MINIMIZE</code>, <code>MAXIMAZI_RESTORE</code>,
	 * <code>CLOSE</code>.
	 * @param component the component to be added to this layout
	 * @param constraints the location of the component in this layout
	 */
	public void addLayoutComponent(Component component, Object constraints) {
		synchronized (component.getTreeLock()) {
			if ((constraints == null)  
					|| (constraints instanceof String)) {
				addLayoutComponent((String) constraints, component);
			}
			else {
				throw new IllegalArgumentException("Can not add to layout: constraint must be a string (or null)");
			}
		}
	}

	
	/**
	 * Adds the specify component to the layout, using the specify name as its location.
	 * The constraint should be null or one of the following constants:
	 * <code>ICON</code>, <code>TITLE</code>, <code>MINIMIZE</code>, <code>MAXIMAZI_RESTORE</code>,
	 * <code>CLOSE</code>.
	 * @param name the location of the component in this layout
	 * @param component the component to be added to this layout
	 */
	public void addLayoutComponent(String name, Component component) {
		synchronized (component.getTreeLock()) {
			/* Assign the componentonent to one of the known regions of the layout. */
			if (ICON.equals(name)) {
				icon = component;
			}
			else if (TITLE.equals(name)) {
				title = component;
			}
			else if (MINIMIZE.equals(name)) {
				minimize = component;
			}
			else if (MAXIMIZE_RESTORE.equals(name)) {
				maximize = component;
			}
			else if (CLOSE.equals(name)) {
				close = component;
			}
			else {
				throw new IllegalArgumentException("Can not add to layout:unkown constraint:" +name);
			}
		}
	}

	/**
	 * Lays out the container argument using this layout.
	 * This method actually reshapes the components in the specify container 
	 * in order to satisfy the constraints of this <code>JsTitleBarLayout</code> object.
	 * @param target the container
	 */
	public void layoutContainer(Container target) {
		synchronized (target.getTreeLock()) {
			Insets xIns = target.getInsets();
			int top = xIns.top;
			int left = xIns.left;
			int right = target.getWidth() - xIns.right;
			if (icon != null) {
				Dimension d  = icon.getPreferredSize();
				icon.setBounds(left, top, d.width, d.height);
				left += d.width + hgap;
			}
			if (title != null) {
				Dimension d = title.getPreferredSize();
				title.setBounds(left, top, d.width, d.height);
				left += d.width + hgap;
			}
			if (close != null) {
				Dimension d = close.getPreferredSize();
				close.setBounds(right - d.width, top, d.width, d.height);
				right -= d.width + hgap;
			}
			if (maximize != null) {
				Dimension d = maximize.getPreferredSize();
				maximize.setBounds(right - d.width, top, d.width, d.height);
				right -= d.width + hgap;
			}
			if (minimize != null) {
				Dimension d = minimize.getPreferredSize();
				minimize.setBounds(right - d.width, top, d.width, d.height);
				right -= d.width + hgap;
			}
		}
	}

	/**
	 * Determines the minimum size of the target container using this layout manager.
	 * This method is called when a container calls its getMinimumSize method.
	 * Most applications do no call this method directly.
	 * @param target the container
	 * @return teh minimum size of this layout
	 */
	public Dimension minimumLayoutSize(Container target) {
		synchronized (target.getTreeLock()) {
			Dimension xDmn = new Dimension(0, 0);
			if (icon != null) {
				Dimension d = icon.getMinimumSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (title != null) {
				Dimension d = title.getMinimumSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (minimize != null) {
				Dimension d = minimize.getMinimumSize();
				xDmn.width += d.width;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (maximize != null) {
				Dimension d = maximize.getMinimumSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (close != null) {
				Dimension d = close.getMinimumSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			Insets xIns = target.getInsets();
			xDmn.width += xIns.left + xIns.right;
			xDmn.height += xIns.top + xIns.bottom;
			return xDmn;
		}
	}
	
	/**
	 * Determines the preferred size of the target container using this layout manager, 
	 * based on the components in the container.
	 * This method is called when a container calls its getPreferredSize method.
	 * Most applications do not call this method directly.
	 * @param target the container
	 * @return the preferred size of this layout
	 */
	public Dimension preferredLayoutSize(Container target) {
		synchronized (target.getTreeLock()) {
			// north line
			Dimension xDmn = new Dimension(0, 0);
			if (icon != null) {
				Dimension d = icon.getPreferredSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (title != null) {
				Dimension d = title.getPreferredSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (minimize != null) {
				Dimension d = minimize.getPreferredSize();
				xDmn.width += d.width;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (maximize != null) {
				Dimension d = maximize.getPreferredSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			if (close != null) {
				Dimension d = close.getPreferredSize();
				xDmn.width += d.width + hgap;
				xDmn.height = Math.max(xDmn.height, d.height);
			}
			Insets xIns = target.getInsets();
			xDmn.width += xIns.left + xIns.right;
			xDmn.height += xIns.top + xIns.bottom;
			return xDmn;
		}
	}

	/**
	 * Removes the specify component from this layout.
	 * This method is called when a container calls its remove or removeAll methods.
	 * Most applications do not call this method directly.
	 * @param component the component to be removed from this layout
	 */
	public void removeLayoutComponent(Component component) {
		synchronized (component.getTreeLock()) {
			if (component == icon) {
				icon = null;
			}
			else if (component == title) {
				title = null;
			}
			else if (component == minimize) {
				minimize = null;
			}
			else if (component == maximize) {
				maximize = null;
			}
			else if (component == close) {
				close = null;
			}
		}
	}
	
	/**
	 * Returns the constraints associated with the specify component.
	 * @param component the component
	 * @return the constraints indicating the location of the given component in this layout
	 */
	public Object getConstraints(Component component) {
		if (component == icon) {
			return ICON;
		}
		else if (component == title) {
			return TITLE;
		}
		else if (component == minimize) {
			return MINIMIZE;
		}
		else if (component == maximize) {
			return MAXIMIZE_RESTORE;
		}
		else if (component == close) {
			return CLOSE;
		}
		else {
			return null;
		}
	}
	
	/**
	 * Returns a representation of this layout.
	 * @return the string specifying this layout
	 */
	public String toString() {
		return getClass().getName() + "[hgap=" + hgap + "]";
	}

}

⌨️ 快捷键说明

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