How to center a Jframe on the computer screen?

Asked

Viewed 1,440 times

1

I’m using the GUI Builder Netbeans to do some things with Swing and would like to centralize the JFrame, in which is my application, on the computer screen. However, do not know where I edit this part.

Where is it when the program runs:

Where I want it to be (more or less) when the program runs:

1 answer

4


Use Window#setLocationRelativeTo() passing the value 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);
   }
}
  • An addendum should be called after the window has been completely built. Preferably, before setting as visible.

  • So called after initComponents.

  • 'Cause I usually leave it to main do this(after the class has become standard) as a precaution.

  • Is there any way to use this same code in a @Renan method?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.