Java tutorial
//package com.java2s; /* * Copyright 2005-2006 UniVis Explorer development team. * * This file is part of UniVis Explorer * (http://phobos22.inf.uni-konstanz.de/univis). * * UniVis Explorer 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. * * Please see COPYING for the complete licence. */ import java.awt.*; public class Main { /** * Centers a component on screen. * * @param c The component that will be centered. */ public static void centerComponentOnScreen(Component c) { // Size of the screen. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight(); // Size of the component. Dimension size = c.getSize(); width -= size.getWidth(); height -= size.getHeight(); c.setLocation((int) width / 2, (int) height / 2); } }