Java JFrame Center centerFrame(final JFrame target)

Here you can find the source of centerFrame(final JFrame target)

Description

Centers JFrame on screen

License

Apache License

Parameter

Parameter Description
target a parameter

Declaration

public static void centerFrame(final JFrame target) 

Method Source Code


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

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

public class Main {
    /**// w  ww .  j a v  a 2s .c o  m
     * Centers {@link JFrame} on screen
     *
     * @param target
     */
    public static void centerFrame(final JFrame 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;
    }
}

Related

  1. centerDialog(final JFrame frame, JDialog dialog)
  2. centerDialog(JDialog dialog, JFrame frame)
  3. centerDialog(JDialog dialog, JFrame parent)
  4. centerDialogIntoFrame(java.awt.Component p_CompToBePositioned, javax.swing.JFrame p_MainFrame)
  5. centerDialogOnFrame(JFrame parentFrame, JDialog dialog)
  6. centerFrame(JFrame frame)
  7. centerFrame(JFrame frame)
  8. centerFrame(JFrame frame)
  9. centerFrame(JFrame frame)