Center Swing Window

3

I wanted to know how to do when to call a secondary window in Swing it left centered with the main window of the program. At the moment, when I call it it appears in the upper left corner.

    
asked by anonymous 23.09.2015 / 13:46

2 answers

4

Try this:

frame.setLocationRelativeTo(null);  
    
23.09.2015 / 13:48
2

You can center a frame by using the setLocationRelativeTo () method. Here's an 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.Procure (from a control + f) by the setLocationRelativeTo method.

    
23.09.2015 / 14:58