Here you can find the source of centerComponent(final JComponent target)
Parameter | Description |
---|---|
target | a parameter |
public static void centerComponent(final JComponent target)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.awt.*; public class Main { /**//from www . j a v a2 s .c o m * 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; } }