Java tutorial
//package com.java2s; import java.awt.Dimension; import java.awt.Frame; import java.awt.Toolkit; import java.awt.Window; public class Main { /** * Centers a Frame in the middle of the screen. Also sets minimum size * as 30% of the entire screen and preferred size as 80% of the entire * screen. * * @param frame */ public static void centerWindow(Frame frame) { centerWindow(frame, 0); } public static void centerWindow(Frame frame, int size) { centerWindow(frame, size, size); } public static void centerWindow(Frame frame, int width, int height) { centerWindow((Window) frame, width, height); } public static void centerWindow(Window window, int width, int height) { // Get screen size final Toolkit tk = Toolkit.getDefaultToolkit(); final Dimension screensize = tk.getScreenSize(); // Set window minimum size window.setMinimumSize(new Dimension((int) Math.floor(screensize.getWidth() * 0.3), (int) Math.floor(screensize.getHeight() * 0.3))); // Set window size if (width == 0f) width = (int) Math.floor(screensize.getWidth() * 0.8); if (height == 0f) height = (int) Math.floor(screensize.getHeight() * 0.8); window.setPreferredSize(new Dimension(width, height)); int left = (int) (screensize.getWidth() - width) / 2; int right = (int) (screensize.getHeight() - height) / 2; ; // Center window window.setLocation(left, right); } }