3

I would like to know if there is an option to center a jframe to the center of the screen(monitor)? I have tried with co-ordinates but its not exact at the center. This is what I have:

 Dimension ss = Toolkit.getDefaultToolkit ().getScreenSize ();
        Dimension frameSize = new Dimension ( 800, 300 );
        frame1.setBounds ( ss.width / 2 - frameSize.width / 2, 
                          ss.height / 2 - frameSize.height / 2,
                          frameSize.width, frameSize.height );

PS:If the question is repeated please post the link.

Thank you.

Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52

1 Answers1

4
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

pack() gets all the layout managers to lay out components of the GUI. The setLocationRelativeTo(null) centers the GUI.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373