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

📄 jsframelayout.java

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

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

import com.hfkj.jsframe.layout.JsLayoutManager;

/**
 * A js frame layout lays out a container, arranging and resizing its components to
 * fit in thirteen regions: northwest, north, northeast, west, title, title line, menu,
 * content, state, east, southwest, south, southeas. Each region may contain no more than
 * one component, and is identified by a corresponding constraint: 
 * <code>NORTHWEST</code>, <code>NORTH</code>, <code>NORTHEAST</code>, <code>WEST</code>,
 * <code>TITLE</code>, <code>TITLE_LINE</code>, <code>MENU</code>, <code>CONTENT</code>,
 * <code>STATE</code>, <code>EAST</code>, <code>SOUTHWEST</code>, <code>SOUTH</code>,
 * <code>SOUTHEAST</code>.
 * When adding a component to a container with a js frame layout, use one of the above thirteen
 * constraints, for example:
 * <pre>
 * Panel p = new Panel();
 * p.setLayout(new JsFrameLayout());
 * p.add(new Button("Okay"), JsFrameLayout.TITLE);
 * </pre>
 * <p>
 * As a convenience, <code>JsFrameLayout</code> interprets the absence of a string specification
 * the same as the constraint <code>CONTENT</code>:
 * <pre>
 * Panel p = new Panel();
 * p.setLayout(new JsFrameLayout());
 * p.add(new Button("Okay"));	// Same as: p.add(new Button("Okay"), JsFrameLayout.CONTENT);
 * </pre>
 * <p>
 * The components are laid out according to their preferred sizes and the constraints of the
 * container's size. The <code>NORTH</code>, <code>TITLE</code>, <code>TITLE_LINE</code>,
 * <code>MENU</code>, <code>STATE</code> and <code>SOUTH</code> components may be stretched horizontally;
 * the <code>WEST</code> and <code>EAST</code> components may be stretched vertically;
 * the <code>CONTENT</code> component may stretch both horizontally and vertically to
 * fill any space left over.
 * <p>
 * Here is an example of thirteen buttons in frame laid out using the <code>JsFrameLayout</code>
 * layout manager:
 * <p>
 * <img src="doc-files/JsFrameLayout.png" alt="Diagram of a frame demonstrating JsFrameLayout.
 * Each section of the JsFrameLayout contains a Button corresponding to its location in the layout,
 * one of: NORTHWEST, NORTH, NORTHEAST, WEST, TITLE, TITLE_LINE, MENU,CONTENT, STATE, EAST,
 * SOUTHWEST, SOUTH, or SOUTHEAST.">
 * <p>
 * The code for this frame is as follows:
 * <p>
 * <pre>
 * import java.awt.Button;
 * import java.awt.Frame;
 * 
 * import com.hfkj.jsframe.frame.JsFrameLayout;
 * 
 * public class Test {
 * 
 * 	public static void main(String[] args) {
 * 		Frame xFrm = new Frame("Test JsFrameLayout");
 * 		xFrm.setUndecorated(true);
 * 		xFrm.setSize(500, 400);
 * 		xFrm.setLocation(200, 100);
 * 		xFrm.setLayout(new JsFrameLayout(5));
 * 		xFrm.add(new Button("Northwest"), JsFrameLayout.NORTHWEST);
 * 		xFrm.add(new Button("North"), JsFrameLayout.NORTH);
 * 		xFrm.add(new Button("Northeast"), JsFrameLayout.NORTHEAST);
 * 		xFrm.add(new Button("West"), JsFrameLayout.WEST);
 * 		xFrm.add(new Button("Title"), JsFrameLayout.TITLE);
 * 		xFrm.add(new Button("Title line"), JsFrameLayout.TITLE_LINE);
 * 		xFrm.add(new Button("Menu"), JsFrameLayout.MENU);
 * 		xFrm.add(new Button("Content"), JsFrameLayout.CONTENT);
 * 		xFrm.add(new Button("State"), JsFrameLayout.STATE);
 * 		xFrm.add(new Button("East"), JsFrameLayout.EAST);
 * 		xFrm.add(new Button("Southwest"), JsFrameLayout.SOUTHWEST);
 * 		xFrm.add(new Button("South"), JsFrameLayout.SOUTH);
 * 		xFrm.add(new Button("Southeast"), JsFrameLayout.SOUTHEAST);
 * 		xFrm.setVisible(true);
 * 	}
 * 		
 * }
 * </pre>
 * 		
 * @version 1.0 01/05/09
 * @author Jason (MSN:www.jason0086.com@hotmail.com)
 */
public class JsFrameLayout implements JsLayoutManager {

	/**
	 * The northwest layout constraint(northwest of container).
	 */
	public static final String NORTHWEST = "Northwest";
	
	/**
	 * The north layout constraint(north of container).
	 */
	public static final String NORTH = "North";
	
	/**
	 * The northeast layout constraint(northeast of container).
	 */
	public static final String NORTHEAST = "Northeast";
	
	/**
	 * The west layout constraint(west of container).
	 */
	public static final String WEST = "West";
	
	/**
	 * The title layout constraint.
	 */
	public static final String TITLE = "Title";
	
	/**
	 * The title line layout constraint.
	 */
	public static final String TITLE_LINE = "Title_Line";
	
	/**
	 * The menu layout constraint.
	 */
	public static final String MENU = "Menu";
	
	/**
	 * The content layout constraint.
	 */
	public static final String CONTENT = "Content";
	
	/**
	 * The state layout constraint.
	 */
	public static final String STATE = "State";
	
	/**
	 * The east layout constraint(east of container).
	 */
	public static final String EAST = "East";
	
	/**
	 * The southwest layout constraint(southwest of container).
	 */
	public static final String SOUTHWEST = "Southwest";
	
	/**
	 * The south layout constraint(south of container).
	 */
	public static final String SOUTH = "South";
	
	/**
	 * The southeast layout constraint(southeast of container).
	 */
	public static final String SOUTHEAST = "Southeast";
	
	/**
	 * Constant to specify components location to be the northwest portion of the js frame layout.
	 */
	private Component northwest = null;
	
	/**
	 * Constant to specify components location to be the north portion of the js frame layout.
	 */
	private Component north = null;
	
	/**
	 * Constant to specify components location to be the northeast portion of the js frame layout.
	 */
	private Component northeast = null;
	
	/**
	 * Constant to specify components location to be the west portion of the js frame layout.
	 */
	private Component west = null;
	
	/**
	 * Constant to specify components location to be the title portion of the js frame layout.
	 */
	private Component title = null;
	
	/**
	 * Constant to specify components location to be the title line portion of the js frame layout.
	 */
	private Component titleLine = null;
	
	/**
	 * Constant to specify components location to be the menu portion of the js frame layout.
	 */
	private Component menu = null;
	
	/**
	 * Constant to specify components location to be the content portion of the js frame layout.
	 */
	private Component content = null;
	
	/**
	 * Constant to specify components location to be the state portion of the js frame layout.
	 */
	private Component state = null;
	
	/**
	 * Constant to specify components location to be the east portion of the js frame layout.
	 */
	private Component east = null;
	
	/**
	 * Constant to specify components location to be the southwest portion of the js frame layout.
	 */
	private Component southwest = null;
	
	/**
	 * Constant to specify components location to be the south portion of the js frame layout.
	 */
	private Component south = null;
	
	/**
	 * Constant to specify components location to be the southeast portion of the js frame layout.
	 */
	private Component southeast = null;
	
	/**
	 * Constriant to specify the vertical gaps between components.
	 */
	private int vgap = 0;
	
	/**
	 * Constructs a new js frame layout with no gaps between components.
	 *
	 */
	public JsFrameLayout() {
		this(0);
	}
	
	/**
	 * Constructs a js frame layout with the specify gaps between components.
	 * The vertical gap is specified by v.
	 * @param v the vertical gap
	 */
	public JsFrameLayout(int v) {
		vgap = v;
	}
	
	/**
	 * Sets the vertical gap between components.
	 * @param v the vertical gap
	 */
	public void setVgap(int v) {
		vgap = v;
	}
	
	/**
	 * Returns the vertical gap between components.
	 * @return the vertical gap
	 */
	public int getVgap() {
		return vgap;
	}
	
	/**
	 * Adds the specify component to the layout, using the specify constraint object.
	 * The constraint should be null or one of the following constriants:
	 * <code>NORTHWEST</code>, <code>NORTH</code>, <code>NORTHEAST</code>, <code>WEST</code>,
	 * <code>TITLE</code>, <code>TITLE_LINE</code>, <code>MENU</code>, <code>CONTENT</code>, <code>STATE</code>,
	 * <code>EAST</code>, <code>SOUTHWEST</code>, <code>SOUTH</code>, <code>SOUTHEAST</code>.
	 * @param component the component to be added to this layout
	 * @param constraints the constraints indicating 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 constraint object.
	 * The name must be null or one of the following constraints:
	 * <code>NORTHWEST</code>, <code>NORTH</code>, <code>NORTHEAST</code>, <code>WEST</code>,
	 * <code>TITLE</code>, <code>TITLE_LINE</code>, <code>MENU</code>, <code>CONTENT</code>, <code>STATE</code>,
	 * <code>EAST</code>, <code>SOUTHWEST</code>, <code>SOUTH</code>, <code>SOUTHEAST</code>.
	 * @param name the location of the specify component to add into this layout
	 * @param component the component to be added to this layout
	 */
	public void addLayoutComponent(String name, Component component) {
		synchronized (component.getTreeLock()) {
			/* Special case: treat null the same as CENTER. */
			if (name == null) {
				name = CONTENT;
			}
			/* Assign the componentonent to one of the known regions of the layout. */
			if (NORTHWEST.equals(name)) {
				northwest = component;
			}
			else if (NORTH.equals(name)) {
				north = component;
			}
			else if (NORTHEAST.equals(name)) {
				northeast = component;
			}
			else if (WEST.equals(name)) {
				west = component;
			}
			else if (TITLE.equals(name)) {
				title = component;
			}
			else if (TITLE_LINE.equals(name)) {
				titleLine = component;
			}
			else if (MENU.equals(name)) {
				menu = component;
			}
			else if (CONTENT.equals(name)) {
				content = component;
			}
			else if (STATE.equals(name)) {
				state = component;
			}
			else if (EAST.equals(name)) {
				east = component;
			}
			else if (SOUTHWEST.equals(name)) {
				southwest = component;
			}
			else if (SOUTH.equals(name)) {
				south = component;
			}
			else if (SOUTHEAST.equals(name)) {
				southeast = component;
			}
			else {
				throw new IllegalArgumentException("Can not add to layout:unkown constraint:" + name);
			}
		}
	}
	
	/**
	 * Returns the component that was added using the given constraint.
	 * The constraint must be null or one of the following constraints:
	 * <code>NORTHWEST</code>, <code>NORTH</code>, <code>NORTHEAST</code>, <code>WEST</code>,
	 * <code>TITLE</code>, <code>TITLE_LINE</code>, <code>MENU</code>, <code>CONTENT</code>, <code>STATE</code>,
	 * <code>EAST</code>, <code>SOUTHWEST</code>, <code>SOUTH</code>, <code>SOUTHEAST</code>.
	 * @param constraints the location of the specify component in this layout
	 * @return the component associated with the given constraints
	 */
	public Component getLayoutComponent(Object constraints) {
		if (NORTHWEST.equals(constraints)) {
			return northwest;
		}
		else if (NORTH.equals(constraints)) {
			return north;
		}
		else if (NORTHEAST.equals(constraints)) {
			return northeast;
		}
		else if (WEST.equals(constraints)) {
			return west;
		}
		else if (TITLE.equals(constraints)) {
			return title;
		}
		else if (TITLE_LINE.equals(constraints)) {
			return titleLine;
		}
		else if (MENU.equals(constraints)) {
			return menu;
		}
		else if (CONTENT.equals(constraints)) {
			return content;
		}
		else if (STATE.equals(constraints)) {
			return state;
		}
		else if (EAST.equals(constraints)) {
			return east;
		}
		else if (SOUTHWEST.equals(constraints)) {
			return southwest;
		}
		else if (SOUTH.equals(constraints)) {
			return south;
		}
		else if (SOUTHEAST.equals(constraints)) {
			return southeast;
		}
		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;
	}

⌨️ 快捷键说明

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