Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dimension; import java.awt.Point; public class Main { private static final int BORDER_SIZE = 50; public static void center(Component c) { Dimension screenSize = c.getToolkit().getScreenSize(); screenSize.width -= BORDER_SIZE; screenSize.height -= BORDER_SIZE; Dimension componentSize = c.getSize(); int xPos = (screenSize.width - componentSize.width) / 2; xPos = Math.max(xPos, 0); int yPos = (screenSize.height - componentSize.height) / 2; yPos = Math.max(yPos, 0); c.setLocation(new Point(xPos, yPos)); } }