相对位置

刚入公司半个月,发现要做的项目是用swing技术,因为公司要求比较严格,只能使用公司提供的eclipse,不能上网下载swing辅助开发工具。所以在布置控件的时候花费比较大的时间,而且有时候会很乱(以某一个控件为参照物,放置在它的上,下,左,右的位置),所以就封装了一个方便使用。如果也有像我那样蛋疼情况的朋友,可以使用一下,如果有什么需要改进的,请多提意见。

public class PUtil {
	final public static int LEFT = 0;
	final public static int LEFT_TOP = 1;
	final public static int LEFT_CENTER = 2;
	final public static int LEFT_BOTTOM = 3;

	final public static int RIGHT = 10;
	final public static int RIGHT_TOP = 11;
	final public static int RIGHT_CENTER = 12;
	final public static int RIGHT_BOTTOM = 13;

	final public static int TOP = 20;
	final public static int TOP_LEFT = 21;
	final public static int TOP_CENTER = 22;
	final public static int TOP_RIGHT = 23;

	final public static int BOTTOM = 30;
	final public static int BOTTOM_LEFT = 31;
	final public static int BOTTOM_CENTER = 32;
	final public static int BOTTOM_RIGHT = 33;

	final public static int INNER_LEFT_TOP = 4;
	final public static int INNER_LEFT_BOTTOM = 5;
	final public static int INNER_RIGHT_TOP = 6;
	final public static int INNER_RIGHT_BOTTOM = 7;

	final public static int RESIZE_TO_LEFT = 50;
	final public static int RESIZE_TO_RIGHT = 51;
	final public static int RESIZE_TO_TOP = 52;
	final public static int RESIZE_TO_BOTTOM = 53;

	// public static Point getRefXY(Component oldC) {
	// return getRefXY(oldC, null, -1, 0);
	// }
	//
	// public static Point getRefXY(Component oldC, int refDirection) {
	// return getRefXY(oldC, null, refDirection, 0);
	// }

	public static Point getRefXY(Component refC, Component newC, int refDirection) {
		return getRefXY(refC, newC, refDirection, 0);
	}

	public static Point getRefXY(Component refC, Component newC, int refDirection, int offset) {
		int value = 0;
		switch (refDirection) {
		case LEFT: // 00
			return new Point(refC.getX() - newC.getWidth() - offset, newC.getY());
		case LEFT_TOP: // 01
			return new Point(refC.getX() - newC.getWidth() - offset, refC.getY());
		case LEFT_CENTER: // 02
			value = (refC.getHeight() - newC.getHeight()) / 2;
			return new Point(refC.getX() - newC.getWidth() - offset, refC.getY() + value);
		case LEFT_BOTTOM: // 03
			value = refC.getHeight() - newC.getHeight();
			return new Point(refC.getX() - newC.getWidth() - offset, refC.getY() + value);

		case RIGHT: // 10
			return new Point(refC.getX() + refC.getWidth() + offset, newC.getY());
		case RIGHT_TOP: // 11
			return new Point(refC.getX() + refC.getWidth() + offset, refC.getY());
		case RIGHT_CENTER: // 12
			value = (refC.getHeight() - newC.getHeight()) / 2;
			return new Point(refC.getX() + refC.getWidth() + offset, refC.getY() + value);
		case RIGHT_BOTTOM: // 13
			value = refC.getHeight() - newC.getHeight();
			return new Point(refC.getX() + refC.getWidth() + offset, refC.getY() + value);

		case TOP: // 20
			return new Point(newC.getX(), refC.getY() - newC.getHeight() - offset);
		case TOP_LEFT:// 21
			return new Point(refC.getX(), refC.getY() - newC.getHeight() - offset);
		case TOP_CENTER: // 22
			value = (refC.getWidth() - newC.getWidth()) / 2;
			return new Point(refC.getX() + value, refC.getY() - newC.getHeight() - offset);
		case TOP_RIGHT: // 23
			value = refC.getWidth() - newC.getWidth();
			return new Point(refC.getX() + value, refC.getY() - newC.getHeight() - offset);

		case BOTTOM: // 30
			return new Point(newC.getX(), refC.getY() + refC.getHeight() + offset);
		case BOTTOM_LEFT: // 31
			return new Point(refC.getX(), refC.getY() + refC.getHeight() + offset);
		case BOTTOM_CENTER: // 32
			value = (refC.getWidth() - newC.getWidth()) / 2;
			return new Point(refC.getX() + value, refC.getY() + refC.getHeight() + offset);
		case BOTTOM_RIGHT: // 33
			value = refC.getWidth() - newC.getWidth();
			return new Point(refC.getX() + value, refC.getY() + refC.getHeight() + offset);

		case INNER_LEFT_TOP: // 4
			return new Point(offset, offset);
		case INNER_LEFT_BOTTOM: // 5
			return new Point(0 + offset, refC.getHeight() - newC.getHeight() - offset - 38); // 38
		case INNER_RIGHT_TOP: // 6
			return new Point(refC.getWidth() - newC.getWidth() - offset - 15, offset); // 15
		case INNER_RIGHT_BOTTOM: // 7
			return new Point(refC.getWidth() - newC.getWidth() - offset - 15, refC.getHeight() - newC.getHeight() - offset - 38);
		default:
			return refC.getLocation();
		}
	}

	public static void setAlign(Component oldC, Component newC, int style) {
		int value = 0;
		switch (style) {
		case RESIZE_TO_LEFT:
			value = Math.abs(oldC.getX() - newC.getX());
			if (newC.getX() < oldC.getX()) {
				newC.setLocation(oldC.getX(), newC.getY());
			} else {
				newC.setSize(newC.getWidth() + value, newC.getHeight());
				newC.setLocation(oldC.getX(), newC.getY());
			}
			break;
		case RESIZE_TO_RIGHT:
			value = Math.abs(oldC.getX()  - newC.getX());
			if (newC.getX() > oldC.getX()) {
				newC.setLocation(oldC.getX(), newC.getY());
			} else {
				newC.setSize(oldC.getWidth() + value  , newC.getHeight());
			}
			break;
		case RESIZE_TO_TOP:
			value = Math.abs(oldC.getY() - newC.getY());
			if (oldC.getY() > newC.getY()) {
				newC.setLocation(newC.getX(), oldC.getY());
			} else {
				newC.setSize(newC.getWidth(), value + newC.getHeight());
				newC.setLocation(newC.getX(), oldC.getY());
			}
			break;
		case RESIZE_TO_BOTTOM:
			value = Math.abs(oldC.getY() - newC.getY());
			if(oldC.getY() < newC.getY()){
				newC.setLocation(newC.getX(), oldC.getY());
			}else{
				newC.setSize(newC.getWidth(), value + newC.getHeight());
			}
			break;
		default:
			break;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值