隱藏的線程實現

來源:互聯網
上載者:User

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;
        }

    }
//不關注的方法被刪除了,沒有寫出來。
    

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.