'How do I center align the components in a Java GUI?

I am a beginner programmer and I'm currently working on a project by making a simple Login screen layout using Java. So far I'm doing pretty good, but is there any way to center-align all the objects I created rather than setting them individually?

enter image description here

So far this is my current code:

package com.main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginScreen extends JFrame {

JLabel lblIntro, lblregister, lblAsGuest; 
JTextField txtUsername;
JPasswordField jpfPassword;
JButton btnLogin, btnRegister, btnAsGuest;
public LoginScreen(){
    super("List n\' Go");
    setLayout(null);

    lblIntro = new JLabel("Welcome to List n\' Go");
    lblregister = new JLabel("Dont have an account yet?");
    lblAsGuest = new JLabel("or continue as Guest");
    txtUsername = new JTextField("Username");
    jpfPassword = new JPasswordField("Password");
    btnLogin = new JButton("Login");
    btnRegister =new JButton("Register");

    lblIntro.setBounds(100,50, 250,40);
    txtUsername.setBounds(50, 150, 300, 40);
    jpfPassword.setBounds(50, 200, 300,  40);
    btnLogin.setBounds(125, 250, 150, 40);
    lblregister.setBounds(125, 350,150, 12);
    btnRegister.setBounds(125, 365, 150, 40);
    lblAsGuest.setBounds(135, 415, 150, 12);

    lblIntro.setFont(new Font("Serif",Font.BOLD, 20));

    add(lblIntro);
    add(txtUsername);
    add(jpfPassword);
    add(btnLogin);
    add(lblregister);
    add(btnRegister);
    add(lblAsGuest);
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent exit){
            System.exit(0);
        }
    });

    setSize(400,600);
    setVisible(true);
    setResizable(false);
}
public static void main(String [] args){
    LoginScreen login = new LoginScreen();
}
}


Solution 1:[1]

You need to look into Layouts. You should never manually set a components bounds or size. Sometimes you need to nest layouts to get the effect your looking for:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    
    Insets insets = new Insets(4, 4, 4, 4);
    JPanel inputPanel = new JPanel(new GridBagLayout());
    inputPanel.add(new JLabel("Username: "), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0, 0));
    inputPanel.add(new JTextField(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    inputPanel.add(new JLabel("Password: "), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0, 0));
    inputPanel.add(new JPasswordField(), new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    
    JPanel buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.add(new JButton("Register"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
    buttonPanel.add(new JButton("Log In"), new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
    
    frame.setLayout(new GridBagLayout());
    frame.add(new JLabel("Welcome to List"), new GridBagConstraints(0, 0, 2, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    frame.add(inputPanel, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    frame.add(buttonPanel, new GridBagConstraints(0, 2, 1, 1, 0, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan