How do I initialize the centralized Jframe?

Asked

Viewed 764 times

1

I’ve tried the this.setLocationRelativeTo(null); only he wasn’t in the middle of the screen, I’d say he was much more right and down than in the center.

public Principal() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
setTitle("Menu Principal");

iniciarTela();

setSize(300, 200);
setVisible(true);
setResizable(false);
}
  • Add your code there. This command centralizes, the problem is elsewhere.

  • Tai mano, I believe you’re in the right way ...

1 answer

2


This is because you are trying to use the method setLocationRelativeTo() to center a screen without having set its size and without having been drawn yet.

Try calling this method after you have set a size for the Frame, as a suggestion, call it before displaying the screen with setVisible() as shown in the code below:

public Principal() {
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setTitle("Menu Principal");

 iniciarTela();

 setSize(300, 200);
 setResizable(false);
 this.setLocationRelativeTo(null);
 setVisible(true);

}
  • 1

    Thanks man, now I understand, I’ll be more attentive when giving these commands, Thank you !

Browser other questions tagged

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