All About Basic


Home | Need Help? | Archives


Java:Open Your Software in the Middle of the Desktop

November 21, 2010 6:33 am

By default Java Programs(GUI based)actually starts/Open  in the side of the desktop.  IF you want your java program/software (where GUI such as JFrame or JPanel is used) to open by default in the middle of the desktop then it’s for you.

Here I have given the code which will help you to open your java (JFrame window) in the middle of the desktop.

Whatever the resolution of your desktop monitor has, this code will help you to open your desire java written software exactly in the middle of the monitor.

Code:

import java.awt.*;

import javax.swing.*;

public class middlePosition extends JFrame

{

private Toolkit  toolkit;

                                                public middlePosition()

                                                {

                                                JPanel panel=new JPanel();

                                                getContentPane().add(panel);

                                                panel.setLayout(null);

                                                setSize(500,500);

                                                setTitle(“Set JFrame Window in the middle of the Desktop”);

                                                setDefaultCloseOperation(EXIT_ON_CLOSE);

                                                toolkit=getToolkit();

                                                Dimension size=toolkit.getScreenSize();

                                                setLocation(size.width/2-getWidth()/2, size.height/2-getHeight()/2);

                                                }

                        public static void main(String args[])

                        {

                        middlePosition x=new middlePosition();

                        x.setVisible(true);

                        }

}

OUTPUT:

Java program in the middle of the desktop

Posted by allaboutbasic

Categories: Java Basic Problems

Tags:

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.