Here you can find the source of centerFrame(JFrame frame, boolean isPopup)
JFrame
to the screen.
Parameter | Description |
---|---|
frame | JFrame frame to center |
isPopup | boolean is the frame a popup dialog? |
public static void centerFrame(JFrame frame, boolean isPopup)
//package com.java2s; /*//from w w w . j av a2 s . co m * CoreUtility.java * Copyright 2002-2003 (C) B. K. Oxley (binkley) * <binkley@alumni.rice.edu> * * 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 * * Created on Februrary 4th, 2002. */ import java.awt.Dimension; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; public class Main { /** * Centers a <code>JFrame</code> to the screen. * * @param frame JFrame frame to center * @param isPopup boolean is the frame a popup dialog? */ public static void centerFrame(JFrame frame, boolean isPopup) { // since the Toolkit.getScreenSize() method is broken in the Linux implementation // of Java 5 (it returns double the screen size under xinerama), this method is // encapsulated to accomodate this with a hack. // TODO: remove the hack, once Java fixed this. // final Dimension screenSize = getScreenSize(Toolkit.getDefaultToolkit()); final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().getBounds(); if (isPopup) { frame.setSize(screenSize.width / 2, screenSize.height / 2); } final Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation(screenSize.x + (screenSize.width - frameSize.width) / 2, screenSize.y + (screenSize.height - frameSize.height) / 2); } }