Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; public class Main { public static final void center(Component component) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = component.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } component.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); } }