Java JFrame Center centerFrame(JFrame frame)

Here you can find the source of centerFrame(JFrame frame)

Description

center Frame

License

Open Source License

Declaration

public static void centerFrame(JFrame frame) 

Method Source Code


//package com.java2s;
// GNU General Public License:                                                //

import java.awt.*;

import javax.swing.*;

public class Main {
    private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public static void centerFrame(JFrame frame) {
        Dimension frameSize = frame.getSize();
        frame.setLocation(what(frameSize.width, screenSize.width), what(frameSize.height, screenSize.height));
        frame.setVisible(true);/*from   www .  j  ava  2s  .c om*/
    }

    private static int what(int forgndLen, int bkgndLen, int offset) {
        int x = offset + (bkgndLen - forgndLen) / 2;
        return (x > 0 ? x : 0);
    }

    private static int what(int forgndLen, int bkgndLen) {
        return what(forgndLen, bkgndLen, 0);
    }
}

Related

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