Java Swing實現類似QQ的停靠在桌面邊緣時自動隱藏

d6nd 9年前發布 | 2K 次閱讀 Java

Java Swing實現類似QQ的停靠在桌面邊緣時自動隱藏

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame; import javax.swing.Timer;

public class HideFrame extends JFrame implements ActionListener{ private Rectangle rect; private int frameLeft;// 窗體離屏幕左邊的距離 private int frameRight;// 窗體離屏幕右邊的距離; private int frameTop;// 窗體離屏幕頂部的距離 private int frameWidth; // 窗體的寬 private int frameHeight; // 窗體的高

private int screenXX;// 屏幕的寬度;
private Point point;    // 鼠標在窗體的位置

private Timer timer = new Timer(10, this);
private int xx, yy;
private boolean isDraging = false;

public HideFrame(){
    timer.start();
    setTitle("窗體在屏幕邊緣隱藏演示");
    setSize(400, 300);
    setUndecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            isDraging = true;
            xx = e.getX();
            yy = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            isDraging = false;
        }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            if (isDraging) {
                int left = getLocation().x;
                int top = getLocation().y;
                setLocation(left + e.getX() - xx, top + e.getY() - yy);
                repaint();
            }
        }
    });
    setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    frameLeft = getLocationOnScreen().x;
    frameTop = getLocationOnScreen().y;
    frameWidth = getWidth();
    frameHeight = getHeight();
    screenXX = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
    frameRight =screenXX- frameLeft - frameWidth;

    // 獲取窗體的輪廓
    rect = new Rectangle(0, 0, frameWidth, frameHeight);
    // 獲取鼠標在窗體的位置
    point =getMousePosition();

    if (frameLeft < 0 && isPtInRect(rect, point)) {
        setLocation(0, frameTop); // 隱藏在左邊,鼠標指到后顯示窗體;
    } else if (frameLeft > -5 && frameLeft < 5 && !(isPtInRect(rect, point))) {
        setLocation(frameLeft - frameWidth + 1, frameTop); // 窗體移到左邊邊緣隱藏到左邊;
    } else if ((frameTop < 0 && frameLeft < 0) && isPtInRect(rect, point)) {// 窗體在左上角;
        setLocation(0, 0);// 窗口隱藏了,鼠標指到他,就顯示出來;
    } else if ((frameTop > -5 && frameTop < 5) && (frameLeft > -5 && frameLeft < 5) && !(isPtInRect(rect, point))) {
        // 當窗體的上邊框與屏幕的頂端的距離小于5時 ,
        // 并且鼠標不再窗體上將窗體隱藏到屏幕的頂端
        setLocation(frameLeft - frameWidth + 1, 1);
    } else if ((frameTop < 0) && isPtInRect(rect, point)) {
        setLocation(frameLeft, 0);// 窗口隱藏了,鼠標指到他,就顯示出來;
    } else if (frameTop > -5 && frameTop < 5 && !(isPtInRect(rect, point))) {
        // 當窗體的上邊框與屏幕的頂端的距離小于5時 ,
        // 并且鼠標不再窗體上將窗體隱藏到屏幕的頂端
        setLocation(frameLeft, 1 - frameHeight);
    } else if (frameRight < 0 && isPtInRect(rect, point)) {
        setLocation(screenXX - frameWidth + 1, frameTop);// 隱藏在右邊,鼠標指到后顯示;
    } else if (frameRight > -5 && frameRight < 5 && !(isPtInRect(rect, point))) {
        setLocation(screenXX - 1, frameTop); // 窗體移到屏幕右邊邊緣隱藏到右邊;
    } else if (frameRight < 0 && frameTop < 0 && isPtInRect(rect, point)) {// 窗體在右上角;
        setLocation(screenXX - frameWidth + 1, 0);// 隱藏在右邊,鼠標指到后顯示;
    } else if ((frameRight > -5 && frameRight < 5) && (frameTop > -5 && frameTop < 5) && !(isPtInRect(rect, point))) {
        setLocation(screenXX - 1, 1); // 窗體移到屏幕右邊邊緣隱藏到右邊;
    }
}

/**
 * 檢測是否在矩形框內
 * @param rect
 * @param point
 * @return
 */
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){
    HideFrame frame = new HideFrame();
}

}</pre>

 本文由用戶 d6nd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!