Swing (javax.swing)

Centering a Frame on the Screen


    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;
    
    frame.setBounds(x, y, w, h);