首页>代码>java swing高仿酷我音乐界面代码例子下载>/ikuwo/src/ikuwo/comm/ui/ViewDragHelper.java
/**
 * 
 */
package ikuwo.comm.ui;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;

import javax.swing.SwingUtilities;

/**
 * 视图拖动辅助类
 */
public class ViewDragHelper {

	private Window fWindow;

	private Component fComponent;
	private int dx, dy;

	/**
	 * 让指定的Component通过鼠标拖动来移动Window
	 */
	public ViewDragHelper(Window window, Component component) {

		fWindow = window;
		fComponent = component;

		fComponent.addMouseListener(createMouseListener());
		fComponent.addMouseMotionListener(createMouseMotionListener());
	}

	private MouseListener createMouseListener() {
		return new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				Point clickPoint = new Point(e.getPoint());
				SwingUtilities.convertPointToScreen(clickPoint, fComponent);
				dx = clickPoint.x - fWindow.getX();
				dy = clickPoint.y - fWindow.getY();
				// fWindow.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}

			public void mouseReleased(MouseEvent e) {
				fWindow.setCursor(Cursor.getDefaultCursor());
			}
		};
	}

	private MouseMotionAdapter createMouseMotionListener() {
		return new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				Point dragPoint = new Point(e.getPoint());
				SwingUtilities.convertPointToScreen(dragPoint, fComponent);

				fWindow.setLocation(dragPoint.x - dx, dragPoint.y - dy);
			}
		};
	}
}
最近下载更多
cxz2132132  LV11 2023年12月9日
xinxin23383  LV4 2023年11月28日
香菇肉饼汤  LV8 2023年11月28日
xp95323  LV14 2023年11月27日
初心不负丶方得始终  LV10 2023年11月27日
wyx065747  LV67 2022年5月7日
liys1234  LV9 2022年4月27日
微信网友_5852742079762432  LV6 2022年4月11日
一个好人520  LV10 2021年9月30日
Mayoubin2001  LV21 2021年6月10日
最近浏览更多
woldxy  LV12 4月1日
cxz2132132  LV11 2023年12月9日
xinxin23383  LV4 2023年11月28日
香菇肉饼汤  LV8 2023年11月28日
15817213218  LV12 2023年11月28日
fesfefe  LV13 2023年11月27日
初心不负丶方得始终  LV10 2023年11月27日
Eayon  LV19 2023年11月27日
Ji123455  LV8 2023年9月21日
zhaozhiwen123 2023年9月18日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友