关于java下实现窗口贴边自动缩进

最近要做一个类似于人人的大作业,于是有了让窗口贴边自动缩进这个想法,然后具体代码感谢qusic童鞋,基本基于他提供的代码实现

这个是主类:

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * @author qusic
 *
 */
public class AutoHiddenFrame extends JFrame
{
	public static void main(String args[])
	{
		new AutoHiddenFrame();
	}
	
	
	public static final int NORMAL = 0;			//窗体的普通状态
	public static final int CANHIDD = 1;		//窗体位于屏幕边缘,可以隐藏的状态	
	public static final int HIDDEN = 2;			//窗体处于隐藏状态
	
	private int state = NORMAL;			//窗体的状态,让它初始化为普通状态
	private Point hiddenPoint;			//隐藏窗体时,窗体的位置
	private Point visiblePoint;			//窗体处于显示状态时的位置	
	
	private JLabel infoLabel;				//用于显示信息的JLabel;
	public AutoHiddenFrame()
	{
		JPanel p = new JPanel(new BorderLayout()) {
			public Insets getInsets()
			{
				return new Insets(3,3,3,3);
			}
		};
		setContentPane(p);	//替换掉原来的ContentPane,换上一个带有Insets的,至于为什么去看WindowMouseListener类
		infoLabel = new JLabel();
		add(infoLabel,BorderLayout.SOUTH);
		setSize(300,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		
		new WindowLocationListener(this);
		new WindowMouseListener(this);
		WindowLocationListener.checkAutoHiddenState(this);//刚出来就检查一下窗体的位置
	}
	
	/**
	 * @param newState 新的状态
	 * 一定要是此类中定义的3中状态之一
	 */
	public void setStates(int newState)
	{
		if(newState == NORMAL || newState == CANHIDD || newState == HIDDEN)
		{
			state = newState;
		}
	}
	
	/*
	 * 返回状态,注意此方法和setStates方法区别与JFrame中的setState()和getState()方法
	 */
	public int getStates()
	{
		return state;
	}
	/*
	 * 设置要显示时窗体的坐标
	 */
	public void setVisiblePoint(Point point)
	{
		visiblePoint = point;
	}
	/*
	 * 设置要隐藏是窗体的坐标
	 */
	public void setHiddenPoint(Point point)
	{
		hiddenPoint = point;
	}
	
	
	public void moveToVisible()
	{
		if(visiblePoint!=null)
		{			
			WindowMover.moveToPoint(this, visiblePoint);
			setStates(CANHIDD);
		}		
	}
	
	public void moveToHidden()
	{
		if(hiddenPoint!=null)
		{
			WindowMover.moveToPoint(this, hiddenPoint);
			setStates(HIDDEN);
		}
	}
	
	public void showInfo(String info)
	{
		infoLabel.setText(info);
	}
	public void clearInfo()
	{
		infoLabel.setText("");
	}
}

检查窗体位置:

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class WindowLocationListener extends ComponentAdapter
{
	public static final int	HIDDEN_BOUND	= 3;		//当窗体进入到屏幕边缘3像素以内就可以隐藏
	
	public static final int VISIBLE_BOUND = 5;		//当窗体隐藏后要有5像素的部分露出来,不能完全隐藏

	AutoHiddenFrame					frame;

	public WindowLocationListener(AutoHiddenFrame a)
	{
		frame = a;
		frame.addComponentListener(this);
	}

	public void componentMoved(ComponentEvent e)
	{
		checkAutoHiddenState(frame);
		//当窗体移动就调用检查方法;
	}

	public static void checkAutoHiddenState(AutoHiddenFrame frame)
	{
		//当窗体状态不是隐藏的,再进行检查
		if (frame.getStates() != AutoHiddenFrame.HIDDEN)
		{
			//首先获得屏幕的大小和窗体的坐标
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Point hiddenPoint = frame.getLocation();			
			
			Point visiblePoint = null;
			boolean canhidden = false;
			//当窗体位于左边边缘
			if (hiddenPoint.x <= HIDDEN_BOUND)
			{				
				hiddenPoint.move(VISIBLE_BOUND - frame.getWidth(), hiddenPoint.y);
				visiblePoint = new Point(0 , hiddenPoint.y);
				canhidden = true;
			}
//		当窗体位于上边
			else if (hiddenPoint.y <= HIDDEN_BOUND)
			{
				hiddenPoint.setLocation(hiddenPoint.x,VISIBLE_BOUND - frame.getHeight());	
				visiblePoint = new Point( hiddenPoint.x , 0 );
				canhidden = true;
			}
//		当窗体位于右边
			else if (hiddenPoint.x + frame.getWidth() >= screenSize.width - HIDDEN_BOUND)
			{
				hiddenPoint.setLocation(screenSize.width - VISIBLE_BOUND, hiddenPoint.y);
				visiblePoint = new Point(screenSize.width - frame.getWidth(), hiddenPoint.y);
				canhidden = true;
			}
			if(canhidden)
			{
				//如果符合以上几种情况的一种就可以隐藏
				frame.setVisiblePoint(visiblePoint);
				frame.setHiddenPoint(hiddenPoint);
				frame.setStates(AutoHiddenFrame.CANHIDD);
				frame.showInfo("进入可隐藏区域!");
			}else {
				//如果不可以隐藏,那就是离开了边缘了
				if(frame.getStates() == AutoHiddenFrame.CANHIDD)
				{
					frame.showInfo("离开可应藏区域!");
				}
				frame.setVisiblePoint(frame.getLocation());
				frame.setStates(AutoHiddenFrame.NORMAL);
			}
		}
	}
}

检查鼠标是否位于窗体上:

import java.awt.Container;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class WindowMouseListener extends MouseAdapter implements ActionListener ,WindowFocusListener
{
	private Timer						timer;

	private AutoHiddenFrame	frame;

	Container								container;

	public WindowMouseListener(AutoHiddenFrame a)
	{
		frame = a;
		container = frame.getContentPane();
		container.addMouseListener(this);
		//注册鼠标侦听器到ContentPane上,因为我们可以加大它的Insets以提高鼠标进入和离开的灵敏度
		frame.addWindowFocusListener(this);
		//注册一个焦点侦听器到窗体上
		timer = new Timer(2000, this);
		timer.setRepeats(false);	
	}

	public void mouseEntered(MouseEvent e)
	{
		//当鼠标进入,就显示窗体
		if (frame.getStates() == AutoHiddenFrame.HIDDEN)
		{
			frame.moveToVisible();
		}else {
			frame.clearInfo();
		}

	}

	public void mouseExited(MouseEvent e)
	{
		//当鼠标离开,启动计时器
		if (frame.getStates() == AutoHiddenFrame.CANHIDD)
		{			
			if (!container.contains(e.getPoint()))
			{
				frame.showInfo(timer.getDelay()/1000+"秒后自动隐藏窗口!");
				//System.out.println("Mouse has exited the Dialog!");
				timer.restart();
			}
		}

	}

	public void actionPerformed(ActionEvent e)
	{
		//计时器到期,检查鼠标是不是还在此窗体里面,不再的话,再开始隐藏
		Point p = MouseInfo.getPointerInfo().getLocation();
		SwingUtilities.convertPointFromScreen(p, container);		
		if (!container.contains(p)
				&& frame.getStates() == AutoHiddenFrame.CANHIDD)
		{
			frame.moveToHidden();
		}else
		{
			frame.clearInfo();
		}
	}

	public void windowGainedFocus(WindowEvent e)
	{
		//得到焦点检查鼠标是不是在窗体上
		Point p = MouseInfo.getPointerInfo().getLocation();
		SwingUtilities.convertPointFromScreen(p, container);
		if (container.contains(p)	&& frame.getStates() == AutoHiddenFrame.HIDDEN)
		{
			frame.moveToVisible();
		}
	}

	public void windowLostFocus(WindowEvent e)
	{
		//失去焦点,启动计时器
		if(frame.getStates() == AutoHiddenFrame.CANHIDD)
		{
			frame.showInfo("2秒后自动隐藏窗口!");
			timer.restart();
		}		
	}
}

最后是窗体移动:

import java.awt.Point;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

public class WindowMover extends Thread
{
	
	public static void moveToPoint(JFrame dialog,Point targetPoint)
	{
		new WindowMover(dialog,targetPoint).start();
	}
	
	
	private static final int	FRAMES	= 15;	//最多移动多少动画帧

	private JFrame						window;			// 要移动的窗口

	private Point							point;				// 目的坐标

	private int								index;			//当前帧数

	private int								addedX;			//每次移动的X坐标增量

	private int								addedY;			//每次移动的Y坐标的增量

	ComponentListener componentListeners[];//组件侦听器数组
	
	
	/*
	 * 定义私有的构造方法,应调用静态方法moveToPoint;
	 */
	private WindowMover(JFrame window, Point targetPoint)
	{
		this.window = window;
		window.getGlassPane().setVisible(true);
		//设置此窗体的GlassPane为显示的,以阻止子组件接收鼠标事件,减少事件触发
		

		//同样,移除此窗体上的组件侦听器,防止再次触发窗体移动事件
		componentListeners = window.getComponentListeners();

		for(ComponentListener cl:componentListeners)
		{	
			window.removeComponentListener(cl);
		}

		Point wl = window.getLocation();
		point = targetPoint;
		index = 0;	//初始化帧书为0;
		
		//计算每次移动量
		addedX = (point.x - wl.x) / FRAMES;
		if (addedX == 0 && point.x != wl.x)
		{
			addedX = point.x < wl.x ? -1 : 1;
		}
		addedY = (point.y - wl.y) / FRAMES;
		if (addedY == 0 && point.y != wl.y)
		{
			addedY = point.y < wl.y ? -1 : 1;
		}
	}


	public void run()
	{

		if (window.getLocation().equals(point))
			return;// 如果已在目的点,则返回
		if (!window.isVisible())
			return;// 如果窗口是不可视的则返回	
	
		while (index < FRAMES)
		{
			Point p = window.getLocation();
			if (p.x != point.x)
				p.translate(addedX, 0);
			if (p.y != point.y)
				p.translate(0, addedY);
			window.setLocation(p);
			index++;
			try
			{
				Thread.sleep(15);

			} catch (Exception e)
			{
			}
		}		
		window.setLocation(point);
		//还原所做的操作
		window.getGlassPane().setVisible(false);
		for(ComponentListener cl:componentListeners)
		{	
			window.addComponentListener(cl);
		}
		//释放资源,使gc可以回收此对象
		window = null;
		point = null;
		componentListeners =null;
		System.out.println("finsh Moved");
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF仿QQ贴边自动隐藏功能主要是通过设置窗口的动画效果来实现的。首先,需要监听窗口的位置变化事件,当窗口靠近屏幕边缘时,触发隐藏动画效果。其次,可以使用WPF自带的动画库,如DoubleAnimation、Storyboard等,来定义窗口隐藏时的动画效果。在动画完成后,将窗口的Visibility属性设置为Collapsed来实现窗口的隐藏。 下面是实现这一功能的一段简单源码示例: ```csharp private void Window_LocationChanged(object sender, EventArgs e) { double screenWidth = SystemParameters.PrimaryScreenWidth; double screenHeight = SystemParameters.PrimaryScreenHeight; double pixelsFromTop = this.Top; double pixelsFromBottom = screenHeight - this.Top - this.ActualHeight; double pixelsFromLeft = this.Left; double pixelsFromRight = screenWidth - this.Left - this.ActualWidth; double boundary = 10; // 靠边触发隐藏的距离阈值 if (pixelsFromTop < boundary || pixelsFromBottom < boundary || pixelsFromLeft < boundary || pixelsFromRight < boundary) { // 如果靠近边缘,执行隐藏动画 DoubleAnimation animation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5))); this.BeginAnimation(UIElement.OpacityProperty, animation); } else { // 如果不靠近边缘,取消隐藏动画 DoubleAnimation animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.5))); this.BeginAnimation(UIElement.OpacityProperty, animation); } } ``` 这段代码中,通过监听窗口的LocationChanged事件,计算窗口与屏幕边缘的距离,并根据一定的阈值来触发隐藏动画效果。当窗口靠近边缘时,执行隐藏动画,动画完成后将窗口的Opacity属性设置为0,实现窗口的隐藏。当窗口远离边缘时,取消隐藏动画,并将窗口的Opacity属性设置为1,使窗口重新显示出来。通过这种方式,就可以实现仿QQ贴边自动隐藏的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值