How to center a JFrame on the computer screen?

1

I am using the NetBeans builder GUI to do some things with Swing and would like to center the JFrame , which is my application, on the computer screen. However, I do not know where I edit this part.

Where is it when the program runs:

WhereIwanttobe(moreorless)whentheprogramexecutes:

    
asked by anonymous 08.04.2016 / 21:39

1 answer

3

Use Window#setLocationRelativeTo() by passing null as argument. According to the documentation:

  

If the component is null , or the GraphicsConfiguration associated with this component is null , the window is placed in the center of the screen.

You can call this method after the initComponents generated by NetBeans.

public class MinhaClasse extends JFrame {

   public MinhaClasse(){
     initComponents(); // Método gerado pelo NetBeans.
     meujFrame.setLocationRelativeTo(null);
   }
}
    
08.04.2016 / 21:53