Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import javax.swing.*;
import java.awt.*;

public class Main {
    /**
     * Centers JComponent on screen
     *
     * @param target
     */
    public static void centerComponent(final JComponent target) {
        final Rectangle screen = getScreenRect();
        final Rectangle frameSize = target.getBounds();
        final int x = (screen.width - frameSize.width) / 2;
        final int y = (screen.height - frameSize.height) / 2;
        target.setLocation(x, y);
    }

    /**
     * Gers screen rectangle
     *
     * @return
     */
    private static Rectangle getScreenRect() {
        final Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().getBounds();
        return screen;
    }
}