Here you can find the source of centerFrameOnMainDisplay(final JFrame frame)
Parameter | Description |
---|---|
frame | The frame to center |
public static void centerFrameOnMainDisplay(final JFrame frame)
//package com.java2s; //License from project: Open Source License import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; public class Main { /**// www. ja va2s . c o m * Center a frame on the main display * * @param frame * The frame to center */ public static void centerFrameOnMainDisplay(final JFrame frame) { final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsDevice[] screens = ge.getScreenDevices(); if (screens.length < 1) { return; // Silently fail. } final Rectangle screenBounds = screens[0].getDefaultConfiguration().getBounds(); final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2); final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2); frame.setLocation(x, y); } }