Center Swing Window

Asked

Viewed 1,346 times

3

I wanted to know how to call a Secondary Window in Swing and leave it centered with the Main Window of the program. At the moment, when I call him appears in the upper left corner.

2 answers

4


Try it this way:

frame.setLocationRelativeTo(null);  
  • 1

    Thank you very much man, it worked at first.

  • For nothing :D. If the answer helped you mark it as correct so that you can help others with the same doubt.

2

You can center a frame through the method setLocationRelativeTo().
Take this example:

import javax.swing.JFrame;

public class MeuFRame {

    public static void main(String[] args) {
    JFrame janela = new JFrame("Frame vazio");
        janela.setSize(300,200);
    janela.setVisible(true);
    janela.setLocationRelativeTo(null); 
    }
}

You can learn more at documentation java. Search (from a control+f) for the method setLocationRelativeTo.

Browser other questions tagged

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