center Window On Screen - Java Swing

Java examples for Swing:Screen

Description

center Window On Screen

Demo Code


//package com.java2s;

import java.awt.Dimension;

import java.awt.Toolkit;
import java.awt.Window;

public class Main {
    public static void centerWindowOnScreen(Window window) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        window.setLocation(// w w w.j a  v a 2  s. c om
                (int) (screenSize.getWidth() / 2 - window.getWidth() / 2),
                (int) (screenSize.getHeight() / 2 - window.getHeight() / 2));
    }
}

Related Tutorials