Java JFrame Center posiToScreenCenter(JFrame frame)

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

Description

Set frame to location screen center.

License

Open Source License

Parameter

Parameter Description
frame a parameter

Declaration

public static void posiToScreenCenter(JFrame frame) 

Method Source Code


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

public class Main {
    /**/*from www .  j av  a 2s .c o  m*/
     * Set frame to location screen center.
     * 
     * @param frame
     * 
     */
    public static void posiToScreenCenter(JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();

        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;

        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }
}

Related

  1. centerWithinDesktop(JInternalFrame frame)
  2. createCenteredJFrame(String title, int width, int height, boolean autoclose)
  3. getCenteringFrame()
  4. getCenterOfTheWindow(JFrame frame)
  5. placeInCenter(JFrame frame)
  6. putWindowInCenter(JFrame window)
  7. SET_FRAME_CENTER(JFrame frame)
  8. sizeAndCenterFrame(JFrame aFrame, int aWidth, int aHeight)