Java tutorial
//package com.java2s; import java.awt.Dimension; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; public class Main { /** * Center the given window within the screen boundaries. * * @param window the window to be centered. */ public static void centerWindow(Window window) { Rectangle bounds; try { bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().getBounds(); } catch (Throwable t) { Toolkit tk = window.getToolkit(); Dimension ss = tk.getScreenSize(); bounds = new Rectangle(ss); } int width = window.getWidth(), height = window.getHeight(); window.setBounds(bounds.x + (bounds.width - width) / 2, bounds.y + (bounds.height - height) / 2, width, height); } }