Java tutorial
//package com.java2s; /* * #%L * VisBio application for visualization of multidimensional biological * image data. * %% * Copyright (C) 2002 - 2014 Board of Regents of the University of * Wisconsin-Madison. * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * 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/gpl-2.0.html>. * #L% */ import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; public class Main { /** Centers the given window on the screen. */ public static void centerWindow(final Window window) { final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); centerWindow(new Rectangle(0, 0, s.width, s.height), window); } /** Centers the given window within the specified parent window. */ public static void centerWindow(final Window parent, final Window window) { centerWindow(parent.getBounds(), window); } /** Centers the given window within the specified bounds. */ public static void centerWindow(final Rectangle bounds, final Window window) { final Dimension w = window.getSize(); int x = bounds.x + (bounds.width - w.width) / 2; int y = bounds.y + (bounds.height - w.height) / 2; if (x < 0) x = 0; if (y < 0) y = 0; window.setLocation(x, y); } }