center Component On Screen - Java Swing

Java examples for Swing:Screen

Description

center Component On Screen

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {

    public static void centerComponentOnScreen(Component c) {

        // Gre der eingestellten Bildschirmaufl?sung.
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        double width = screenSize.getWidth();
        double height = screenSize.getHeight();

        // Gre des <code>JFrame</code>.
        Dimension size = c.getSize();

        width -= size.getWidth();/*from  www .j av  a  2s .co m*/
        height -= size.getHeight();

        c.setLocation((int) width / 2, (int) height / 2);
    }
}

Related Tutorials