swt shell設置窗口位於屏幕中間


 /**
     * 設置窗口位於屏幕中間
     * @param shell 要調整位置的窗口對象
     */
    public static void center(Shell shell)
    {
        //獲取屏幕高度和寬度
        int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
        int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
        //獲取對象窗口高度和寬度
        int shellH = shell.getBounds().height;
        int shellW = shell.getBounds().width;
        
        //如果對象窗口高度超出屏幕高度,則強制其與屏幕等高
        if(shellH > screenH)
            shellH = screenH;
        
        //如果對象窗口寬度超出屏幕寬度,則強制其與屏幕等寬
        if(shellW > screenW)
            shellW = screenW;
        
        //定位對象窗口坐標
        shell.setLocation(((screenW - shellW) / 2), ((screenH - shellH) / 2));
    }
    
    /**
     * 設置窗口位於屏幕中間
     * @param display 設備
     * @param shell 要調整位置的窗口對象
     */
    public static void center(Display display, Shell shell)
    {
        Rectangle bounds = display.getPrimaryMonitor().getBounds();
        Rectangle rect = shell.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        shell.setLocation(x, y);
    } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM