title: Java Swing Login Form
body: <p>I'm trying to create a login form using Java Swing. But I want the layout to look tighter, but still responsive.</p>
  <p><strong>The structure of my project</strong></p>
  <pre><code>Project/
  └── src
      ├── GUI
      │   ├── Control.java
      │   ├── Main.java
      │   └── pages
      │       ├── LoginPanel.java
      └── Helper
          └── Helpers.java
  </code></pre>
  <p><b>Edit</b></p>
  <p>After trying out the answer posted, I got:</p>
  <p><a href="https://i.stack.imgur.com/9pmts.png" rel="nofollow noreferrer">This is the picture</a></p>
  <p>The components in the <code>form</code> panel are not behaving as I would expect</p>
  <p>And my code:</p>
  <p>LoginPanel</p>
  <pre class="lang-java prettyprint-override"><code>package GUI.pages;
  import javax.swing.*;
  
  public class LoginPanel extends JPanel {
  
      private static final int PADDING = 30;
      // Some labels, button, groups and fields
      private static JLabel usernameLabel;
      private static JTextField usernameInputField;
      private static JLabel passwordLabel;
      private static JPasswordField passwordInputField;
      private static JButton submit;
      private static JLabel title = Helpers.generateTitle();
  
  
      public LoginPanel() {
  
          setLayout(new BorderLayout());
          setSize(400, 400);
  
          JPanel titleContainer = new JPanel();
          titleContainer.setLayout(new FlowLayout(FlowLayout.LEFT, PADDING, PADDING));
          titleContainer.add(title);
  
          // the username row
          usernameLabel = new JLabel(&quot;Username: &quot;);
          usernameInputField = new JTextField(50);
  
          // The password row
          passwordLabel = new JLabel(&quot;Password: &quot;);
          passwordInputField = new JPasswordField(150);
  
          JPanel form = new JPanel();
          form.setLayout(new GridBagLayout());
  
          GridBagConstraints c = new GridBagConstraints();
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 0;
          c.gridy = 0;
          c.anchor = GridBagConstraints.WEST;
          c.insets = new Insets(0,20,0,0);
          form.add(usernameLabel, c);
  
          c = new GridBagConstraints();
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 0;
          c.gridy = 1;
          c.anchor = GridBagConstraints.WEST;
          c.insets = new Insets(0,20,0,0);
          form.add(passwordLabel, c);
  
          c = new GridBagConstraints();
          c.gridx = 1;
          c.gridy = 0;
          c.weightx = 0.1;
          c.ipadx = 200;
  
          form.add(usernameInputField, c);
  
          c = new GridBagConstraints();
          c.gridx = 1;
          c.gridy = 1;
          c.weightx = 0.001;
          c.weightx = 0.001;
          c.ipadx = 200;
  
          form.add(passwordInputField, c);
  
          // The message label, the text depends on the query's result.
          message = new JLabel();
  
          // Configuring the submit button
          submit = new JButton(&quot;Sign in&quot;);
          submit.addActionListener(new LoginActionListener());
          submit.setActionCommand(&quot;login&quot;);
  
          // Configuring the redirecting button
          registerRedirect.setActionCommand(&quot;registerRedirect&quot;);
          registerRedirect.addActionListener(new Router());
  
  
          JPanel submitContainer = new JPanel();
          submitContainer.setLayout(new FlowLayout(FlowLayout.RIGHT, PADDING, PADDING));
          submitContainer.add(submit);
  
          form.setMaximumSize(new Dimension(200, passwordInputField.getHeight()));
  
          // adding everything to the layout
  
          add(titleContainer, BorderLayout.PAGE_START);
          add(form);
          add(submitContainer, BorderLayout.PAGE_END);
  
      }
  }
  </code></pre>
  <p>Main</p>
  <pre class="lang-java prettyprint-override"><code>package GUI;
  
  import javax.swing.*;
  import GUI.pages.LoginPanel;
  
  public class Main extends JFrame {
  
      public final static int WINDOW_WIDTH = 750;
      public final static int WINDOW_HEIGHT = 550;
  
      public static void main(String[] args){
          JFrame frame = new Main();
      }
  
      public Main(){
          JPanel login_panel = new LoginPanel();
          add(login_panel);
          setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
      }
  
  }
  </code></pre>
  <p>This is how I want it to look like(updated):</p>
  <pre><code>REM(the title)        
  ________________________________
             __________________
  Username: [__________________]
             __________________
  Password: [__________________]
                        
                     [Submit button]
                      
  </code></pre>