Reputation: 1622
I have to make a login interface.I tried to use the border layout instead of using this model for the frame, but when I go to write setLocationRelativeTo (null), the internal components of the frame are displayed wrong. Some tips for how to set the frame at the center of the screen and the internal components at the center of the screen and displayed well and in specific coordinates?
class Login extends JFrame {
JLabel label1;
final JTextField text1;
public Login(){
setTitle("title");
setLayout(null); //output on left-corner of screen or
setLocationRelativeTo(null); // output on the middle with components displayed wrong
label1 = new JLabel();
label1.setText("text");
text1 = new JTextField(15);
label1.setBounds(100, 100, 100, 20);
text1.setBounds(200, 100, 200, 20);
add(label1);
add(text1);
setVisible(true);
setSize(500, 400);
}public static void main(string [] args){
`
. . . . . .
new Login();}
Upvotes: 1
Views: 285
Reputation: 285405
Your immediate problem can be solved by calling setLocationRelativeTo(null); after calling setSize(...)
setSize(500, 400);
setLocationRelativeTo(null);
But having said that, my actual recommendations are:
setSize(...)
. Let the layout managers do this for you.pack()
before setVisible(true)
setLocationRelativeTo(null);
after pack and before setVisible if you want to center the GUI.Upvotes: 2