Here you can find the source of centerWindow(JFrame frame)
Parameter | Description |
---|---|
frame | frame to centering |
public static void centerWindow(JFrame frame)
//package com.java2s; /*//from www .j a v a 2 s . com * Collab desktop - Software for shared drawing via internet in real-time * Copyright (C) 2012 Martin Indra <aktive@seznam.cz> * * This file is part of Collab desktop. * * Collab desktop is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Collab desktop is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Collab desktop. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.GraphicsEnvironment; import java.awt.Point; import javax.swing.JFrame; public class Main { /** * Move frame to center of current screen * * @param frame frame to centering */ public static void centerWindow(JFrame frame) { Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); frame.setLocation(Math.max(0, centerPoint.x - (frame.getWidth() / 2)), Math.max(0, centerPoint.y - (frame.getHeight() / 2))); } }