Java实现窗体边缘隐藏

 自己写java程序从开始接触到现在有近两年的时间了,一直还没有怎么接触到j2ee方面的知识,比如说那些常用的框架(struts、spring、hibernate),自己都还没接触过。最近做项目又做到与swing相关的东西了。现在又只好来学习学习swing方面的知识,自己对这方面还是比较感兴趣的。

下面的实现窗体隐藏。原文参考这里。该代码实现了对窗体到达屏幕边缘时自动隐藏,并在鼠标经过时自动显示。并能用鼠标随意拖动。

代码如下:

[java] view plaincopy

  1. package com.rosedata;  

  2.   

  3. import java.awt.Point;  

  4. import java.awt.Rectangle;  

  5. import java.awt.event.ActionEvent;  

  6. import java.awt.event.ActionListener;  

  7. import java.awt.event.MouseAdapter;  

  8. import java.awt.event.MouseEvent;  

  9. import java.awt.event.MouseMotionAdapter;  

  10.   

  11. import javax.swing.JFrame;  

  12. import javax.swing.Timer;  

  13.   

  14. public class HideFrame extends JFrame implements ActionListener{  

  15.     private Rectangle rect;  

  16.     private int frameLeft;// 窗体离屏幕左边的距离  

  17.     private int frameRight;// 窗体离屏幕右边的距离;  

  18.     private int frameTop;// 窗体离屏幕顶部的距离  

  19.     private int frameWidth; // 窗体的宽  

  20.     private int frameHeight;    // 窗体的高  

  21.       

  22.     private int screenXX;// 屏幕的宽度;  

  23.     private Point point;    // 鼠标在窗体的位置  

  24.   

  25.     private Timer timer = new Timer(10this);  

  26.     private int xx, yy;  

  27.     private boolean isDraging = false;  

  28.       

  29.     public HideFrame(){  

  30.         timer.start();  

  31.         setTitle("窗体在屏幕边缘隐藏演示");  

  32.         setSize(400300);  

  33.         setUndecorated(true);  

  34.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  35.         setAlwaysOnTop(true);  

  36.         addMouseListener(new MouseAdapter() {  

  37.             public void mousePressed(MouseEvent e) {  

  38.                 isDraging = true;  

  39.                 xx = e.getX();  

  40.                 yy = e.getY();  

  41.             }  

  42.   

  43.             public void mouseReleased(MouseEvent e) {  

  44.                 isDraging = false;  

  45.             }  

  46.         });  

  47.         addMouseMotionListener(new MouseMotionAdapter() {  

  48.             public void mouseDragged(MouseEvent e) {  

  49.                 if (isDraging) {  

  50.                     int left = getLocation().x;  

  51.                     int top = getLocation().y;  

  52.                     setLocation(left + e.getX() - xx, top + e.getY() - yy);  

  53.                     repaint();  

  54.                 }  

  55.             }  

  56.         });  

  57.         setVisible(true);  

  58.     }  

  59.     @Override  

  60.     public void actionPerformed(ActionEvent arg0) {  

  61.         frameLeft = getLocationOnScreen().x;  

  62.         frameTop = getLocationOnScreen().y;  

  63.         frameWidth = getWidth();  

  64.         frameHeight = getHeight();  

  65.         screenXX = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;  

  66.         frameRight =screenXX- frameLeft - frameWidth;  

  67.   

  68.         // 获取窗体的轮廓  

  69.         rect = new Rectangle(00, frameWidth, frameHeight);  

  70.         // 获取鼠标在窗体的位置  

  71.         point =getMousePosition();  

  72.   

  73.         if (frameLeft < 0 && isPtInRect(rect, point)) {  

  74.             setLocation(0, frameTop); // 隐藏在左边,鼠标指到后显示窗体;  

  75.         } else if (frameLeft > -5 && frameLeft < 5 && !(isPtInRect(rect, point))) {  

  76.             setLocation(frameLeft - frameWidth + 1, frameTop); // 窗体移到左边边缘隐藏到左边;  

  77.         } else if ((frameTop < 0 && frameLeft < 0) && isPtInRect(rect, point)) {// 窗体在左上角;  

  78.             setLocation(00);// 窗口隐藏了,鼠标指到他,就显示出来;  

  79.         } else if ((frameTop > -5 && frameTop < 5) && (frameLeft > -5 && frameLeft < 5) && !(isPtInRect(rect, point))) {  

  80.             // 当窗体的上边框与屏幕的顶端的距离小于5时 ,  

  81.             // 并且鼠标不再窗体上将窗体隐藏到屏幕的顶端  

  82.             setLocation(frameLeft - frameWidth + 11);  

  83.         } else if ((frameTop < 0) && isPtInRect(rect, point)) {  

  84.             setLocation(frameLeft, 0);// 窗口隐藏了,鼠标指到他,就显示出来;  

  85.         } else if (frameTop > -5 && frameTop < 5 && !(isPtInRect(rect, point))) {  

  86.             // 当窗体的上边框与屏幕的顶端的距离小于5时 ,  

  87.             // 并且鼠标不再窗体上将窗体隐藏到屏幕的顶端  

  88.             setLocation(frameLeft, 1 - frameHeight);  

  89.         } else if (frameRight < 0 && isPtInRect(rect, point)) {  

  90.             setLocation(screenXX - frameWidth + 1, frameTop);// 隐藏在右边,鼠标指到后显示;  

  91.         } else if (frameRight > -5 && frameRight < 5 && !(isPtInRect(rect, point))) {  

  92.             setLocation(screenXX - 1, frameTop); // 窗体移到屏幕右边边缘隐藏到右边;  

  93.         } else if (frameRight < 0 && frameTop < 0 && isPtInRect(rect, point)) {// 窗体在右上角;  

  94.             setLocation(screenXX - frameWidth + 10);// 隐藏在右边,鼠标指到后显示;  

  95.         } else if ((frameRight > -5 && frameRight < 5) && (frameTop > -5 && frameTop < 5) && !(isPtInRect(rect, point))) {  

  96.             setLocation(screenXX - 11); // 窗体移到屏幕右边边缘隐藏到右边;  

  97.         }  

  98.     }  

  99.       

  100.     /** 

  101.      * 检测是否在矩形框内 

  102.      * @param rect 

  103.      * @param point 

  104.      * @return  

  105.      */  

  106.     public boolean isPtInRect(Rectangle rect, Point point) {  

  107.         if (rect != null && point != null) {  

  108.             int x0 = rect.x;  

  109.             int y0 = rect.y;  

  110.             int x1 = rect.width;  

  111.             int y1 = rect.height;  

  112.             int x = point.x;  

  113.             int y = point.y;  

  114.   

  115.             return x >= x0 && x < x1 && y >= y0 && y < y1;  

  116.         }  

  117.         return false;  

  118.     }  

  119.     public static void main(String[] args){  

  120.         HideFrame frame = new HideFrame();  

  121.     }  

  122. }  


转载于:https://my.oschina.net/sexgirl/blog/350561

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值