Java實現QQ那樣的自動隱藏其實也不是很難。 主要思想就是檢測視窗在螢幕上的位置。當視窗靠邊的時候就重新設定視窗的位置。 導包就省略了…… public class AutoHideFrame extends JFrame implements Runnable, MouseListener { private Thread thread = null; private boolean hide = false; private Toolkit tk = getToolkit(); private final int TOP = 1; private final int RIGHT = 2; private int direction; AutoHideFrame() { thread = new Thread(this); this.setTitle("Auto Hide"); this.setResizable(false); this.setSize(200, 600); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); this.addMouseListener(this); thread.start(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new AutoHideFrame(); } public void run() { // TODO Auto-generated method stub while (true) { if (Thread.currentThread().equals(thread)) { System.out.println("Thread Working..."); double x = getLocation().getX(); //擷取當前視窗在螢幕上的X座標 double y = getLocation().getY();//擷取當前視窗在螢幕上的Y座標 System.out.println("x:=" + x + " y=" + y); // 靠頂隱藏 if (y <= 0) { //判斷視窗靠頂 this.setLocation((int) x, (int) (-getSize().getHeight() + 10)); //重新設定視窗座標。+ 10是為了露出來一點 hide = true; //方便監聽滑鼠移入事件 direction = TOP; } //靠右邊隱藏 if (x >= tk.getScreenSize().getWidth() - getSize().getWidth()) { this.setLocation((int) (tk.getScreenSize().getWidth() - 5), (int) y); hide = true; direction = RIGHT; } } } } public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub if (hide == true) { if (direction == TOP) { this.setLocation((int) getLocation().getX(), 1); } if (direction == RIGHT) { this.setLocation((int) (tk.getScreenSize().getWidth() - getSize().getWidth()-5), (int)getLocation().getY()); } hide = false; } } //不關注的方法被刪除了,沒有寫出來。 } |