Here you can find the source of packAndDisplayInCenterOfScreen(final Window f)
Parameter | Description |
---|---|
f | the window to pack and display |
public static void packAndDisplayInCenterOfScreen(final Window f)
//package com.java2s; /******************************************************************************* * Copyright (C) 2009, 2015, Danilo Pianini and contributors * listed in the project's build.gradle or pom.xml file. * * This file is distributed under the terms of the Apache License, version 2.0 *******************************************************************************/ import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; import javax.swing.SwingUtilities; public class Main { /**// w w w . ja v a2 s. co m * Packs a Window and display it in center of the screen. * * @param f * the window to pack and display */ public static void packAndDisplayInCenterOfScreen(final Window f) { f.pack(); displayInCenterOfScreen(f); } /** * Displays a Component in center of the screen. * * @param f * the component to display */ public static void displayInCenterOfScreen(final Component f) { final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); SwingUtilities.invokeLater(new Runnable() { public void run() { f.setLocation((int) (screenSize.getWidth() - f.getWidth()) / 2, (int) (screenSize.getHeight() - f.getHeight()) / 2); f.setVisible(true); } }); } }