Java Screen Center packAndCenter(Window wind)

Here you can find the source of packAndCenter(Window wind)

Description

Packs and centers the windows

License

Open Source License

Parameter

Parameter Description
wind the windows to pack and center to the screen

Declaration

static public void packAndCenter(Window wind) 

Method Source Code

//package com.java2s;
/*/*from   ww w .j  a  v a2s.c o m*/
 * GuiHelper.java
 * 
 * Copyright (C) 2009 Nicola Roberto Vigan?
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;

public class Main {
    /**
     * Packs and centers the windows
     * @param wind the windows to pack and center to the screen
     */
    static public void packAndCenter(Window wind) {
        wind.pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = wind.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        wind.setSize(frameSize);
        wind.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }
}

Related

  1. getLocationToCenterOnScreen(Component comp)
  2. getPercentageOnScreen(Point location, Dimension size, Rectangle screen)
  3. locateOnScreenCenter(Component component)
  4. moveToCenter(Component componentToMove, Component componentToCenterOver)
  5. moveToScreenCenter(Window w)
  6. packAndCenter(Window window)
  7. screenCenter(Window window)
  8. setCenter(Component comp)
  9. setCenter(Component comp, Component parent)