Java examples for java.awt:Frame
center Frame
import java.awt.Dimension; import java.awt.Frame; import java.awt.GraphicsEnvironment; import java.awt.Point; import java.awt.Rectangle; import javax.swing.JDialog; import org.apache.log4j.Logger; public class Main{ private final static Logger LOG = Logger.getLogger(DialogUtil.class); public static void centerFrame(Frame frame) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); Rectangle screenBounds = ge.getDefaultScreenDevice() .getDefaultConfiguration().getBounds(); Point myLocation = getCenteringPoint(screenBounds, frame.getSize()); frame.setLocation(myLocation);/* w w w .j a va 2s . co m*/ } private static Point getCenteringPoint(Rectangle area, Dimension size) { Point base = area.getLocation(); LOG.debug(String.format("Base: %s", base)); LOG.debug(String.format("dx = %s / 2 - %s / 2", area.getWidth(), size.getWidth())); int dx = (int) (area.getWidth() / 2 - size.getWidth() / 2); LOG.debug(String.format("dy = %s / 2 - %s / 2", area.getHeight(), size.getHeight())); int dy = (int) (area.getHeight() / 2 - size.getHeight() / 2); base.translate(dx, dy); LOG.debug(String.format("dx: %s - dy: %s - translated: %s", dx, dy, base)); return base; } }