歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java像QQ把窗口放到屏幕邊緣並隱藏

Java像QQ把窗口放到屏幕邊緣並隱藏

日期:2017/3/1 11:16:24   编辑:Linux編程

其實可以這麼來做:
1.添加鼠標監聽事件,判定鼠標是否在類似QQ窗口標題欄按下.記錄當前鼠標位置和標題欄右側距離X.
2.在鼠標事件監聽中判定前一條件成立下的鼠標釋放時的標題欄左側位置(記為newX).若newX>屏幕寬度,調用窗口重繪方法(自己定義:即把窗口繪制成一條豎線的樣子,但是必須得有面積,並且保存此時窗口位置)將窗口添加到桌面邊緣並隱藏主窗口.
3.在2中添加一個監聽線程,判定用戶鼠標動作是否落在重繪窗口范圍內,若true則調用主窗口顯示,並且將邊緣窗口隱藏.

上面有個地方打錯了,不好意思.
2中應該是標題欄右側位置newX,不過看你的程序怎麼樣判定啦,無關緊要.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener {

Rectangle rect;
int left;// 窗體離屏幕左邊的距離
int top;// 窗體離屏幕頂部的距離
int width; // 窗體的寬
int height;// 窗體的高
Point point;// 鼠標在窗體的位置
Timer timer = new Timer(10, this);
public static void main(String[] args) {
new MyFrame();
}
public MyFrame() {
timer.start();
this.setTitle("中國");
this.setSize(200, 600);
this.setLocation(700, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
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);
}
}
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;
}
}

Copyright © Linux教程網 All Rights Reserved