Java JFrame Center centerJFrame(JFrame frame)

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

Description

center J Frame

License

Apache License

Declaration

public static void centerJFrame(JFrame frame) 

Method Source Code

//package com.java2s;
/*/*from ww w.  j a v a 2s.  c om*/
 * Copyright 2014 Dominic Masters.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Dimension;
import javax.swing.JFrame;

public class Main {
    public static void centerJFrame(JFrame frame) {
        int width = frame.getWidth();
        int height = frame.getHeight();
        Dimension screen = getScreenSize();
        double x = ((double) screen.width / 2) - ((double) width / 2d);
        double y = ((double) screen.height / 2) - ((double) height / 2d);
        x = Math.round(x);
        y = Math.round(y);
        frame.setLocation((int) x, (int) y);
    }

    public static Dimension getScreenSize() {
        return java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    }
}

Related

  1. centerFrame(Window owner)
  2. centerFrameOnMainDisplay(final JFrame frame)
  3. centerFrameOnScreen(JFrame frame)
  4. centerFrameOnScreen(JFrame frame)
  5. centerFrameTo(final JFrame frameToCenter, final Dimension orientDim)
  6. centerOnFrame(JDialog dialog, JFrame frame)
  7. centerOnFrame(JFrame frame, Component comp)
  8. centerOnParent(JDialog child, JFrame parent)
  9. centerOnScreen(JFrame frame, int screenID)