center Frame - Java Swing

Java examples for Swing:JFrame

Description

center Frame

Demo Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Main {
    public static void centerFrame(JFrame frame) {
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = frame.getSize().width;
        int h = frame.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;

        // Move the window
        frame.setLocation(x, y);/*from  w w w. j  av a  2  s. c  o  m*/
    }
}

Related Tutorials