How do I initialize the centralized JFrame?

1

I have tried this.setLocationRelativeTo(null); only that he was not in the middle of the screen, I would 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);
}
    
asked by anonymous 25.03.2016 / 02:06

1 answer

2

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

Try calling this method after setting 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);

}
    
25.03.2016 / 02:15