Center a big window - Java Swing

Java examples for Swing:Screen

Description

Center a big window

Demo Code

/*/*from  w w  w.j  a v a2s.  co m*/
 **    Copyright (C) 2003-2013 Institute for Systems Biology 
 **                            Seattle, Washington, USA. 
 **
 **    This library is free software; you can redistribute it and/or
 **    modify it under the terms of the GNU Lesser General Public
 **    License as published by the Free Software Foundation; either
 **    version 2.1 of the License, or (at your option) any later version.
 **
 **    This library 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
 **    Lesser General Public License for more details.
 **
 **    You should have received a copy of the GNU Lesser General Public
 **    License along with this library; if not, write to the Free Software
 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
//package com.java2s;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.JFrame;

public class Main {
    /***************************************************************************
     **
     ** Center a big window
     */

    public static Dimension centerBigFrame(JFrame frame, int maxWidth,
            int maxHeight, double scaling, int minHeight) {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int winWidth = screenSize.width - 40;
        int winHeight = screenSize.height - 150;
        if (winWidth > maxWidth) {
            winWidth = maxWidth;
        }
        if (winHeight > maxHeight) {
            winHeight = maxHeight;
        }
        winWidth = (int) (scaling * (double) winWidth);
        winHeight = (int) (scaling * (double) winHeight);
        if (winHeight < minHeight) {
            winHeight = (minHeight > screenSize.height) ? screenSize.height
                    : minHeight;
        }
        frame.setSize(winWidth, winHeight);
        Dimension frameSize = frame.getSize();
        int x = (screenSize.width - frameSize.width) / 2;
        int y = (screenSize.height - frameSize.height) / 2;
        frame.setLocation(x, y);
        return (new Dimension(winWidth, winHeight));
    }
}

Related Tutorials