Java tutorial
//package com.java2s; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; public class Main { public static void center(Window frame) { center(frame, 0, 0, 0, 0); } public static void center(Window frame, int left, int right, int top, int bottom) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > (screenSize.height - top - bottom)) { frameSize.height = screenSize.height - top - bottom; } if (frameSize.width > screenSize.width - left - right) { frameSize.width = screenSize.width - left - right; } if (!frameSize.equals(frame.getSize())) { frame.setSize(frameSize); } frame.setLocation(left + (screenSize.width - frameSize.width) / 2, top + (screenSize.height - frameSize.height) / 2); } }