center JFrame - Java Swing

Java examples for Swing:JFrame

Description

center JFrame

Demo Code


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

public class Main {

    public static void center(JFrame frame) {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        int x = (int) screenSize.getWidth() / 2
                - (int) frameSize.getWidth() / 2;
        int y = (int) screenSize.getHeight() / 2
                - (int) frameSize.getHeight() / 2;
        frame.setLocation(x, y);/*from  w w w .  ja va  2 s .c om*/
    }

    public static Dimension getScreenSize() {
        return Toolkit.getDefaultToolkit().getScreenSize();
    }
}

Related Tutorials