java Jfram自动隐藏

 
 
import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; public class MyFrame extends JFrame implements ActionListener { private Rectangle rect; // 窗体离屏幕左边的距离 private int left; // 窗体离屏幕顶部的距离 private int top; // 窗体的宽 private int width; // 窗体的高 private int height; // 鼠标在窗体的位置 private Point point; private Timer timer = new Timer( 10 , this ); public MyFrame() { timer.start(); } public void actionPerformed(ActionEvent e) { left = getLocationOnScreen().x; top = getLocationOnScreen().y; width = getWidth(); height = getHeight(); // 获取窗体的轮廓 rect = new Rectangle( 0 , 0 , width, height); // 获取鼠标在窗体的位置 point = getMousePosition(); if ((top < 0 ) && isPtInRect(rect, point)) { // 当鼠标在当前窗体内,并且窗体的Top属性小于0 // 设置窗体的Top属性为0,就是将窗口上边沿紧靠顶部 setLocation(left, 0 ); } else if (top > - 5 && top < 5 && ! (isPtInRect(rect, point))) { // 当窗体的上边框与屏幕的顶端的距离小于5时 , // 并且鼠标不再窗体上 将QQ窗体隐藏到屏幕的顶端 setLocation(left, 5 - height); } } /** * 判断一个点是否在一个矩形内 * * @param rect:Rectangle对象 * @param point:Point对象 * @return :如果在矩形内返回true,不在或者对象为null则返回false */ public boolean isPtInRect(Rectangle rect, Point point) { if (rect != null && point != null ) { int x0 = rect.x; int y0 = rect.y; int x1 = rect.width; int y1 = rect.height; int x = point.x; int y = point.y; return x >= x0 && x < x1 && y >= y0 && y < y1; } return false ; } public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setTitle( " Test " ); frame.setSize( 400 , 300 ); frame.setLocation( 400 , 300 ); frame.setVisible( true ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值