set Component Bounds To Center Screen - Java Swing

Java examples for Swing:Screen

Description

set Component Bounds To Center Screen

Demo Code


//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

public class Main {
    public static void setComponentBoundsToCenterScreen(Component component) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int x = (int) (screen.getWidth() - component.getWidth()) / 2;
        int y = (int) (screen.getHeight() - component.getHeight()) / 2;

        component.setBounds(x, y, component.getWidth(),
                component.getHeight());/*from   w w  w . java 2 s  . com*/
    }
}

Related Tutorials