title: GUI in Java (Swing)
body: <p>I'm trying to understand GUI in java. I started with making a simple login page. Here, I'm stacking a heading panel, body panel and a footer panel, in a box layout (Stacked vertically). All this in another parent panel, which is then put into the frame's content pane (center, border layout). However, the body panel, inside the parent panel is getting all stretched up vertically. Having a very tough time fixing it, any suggestions?</p>
  
  <p>I don't want to use spring layout, etc here.</p>
  
  <p>setSize, setMinimumSize, setMaximumSize don't seem to be working here at all.</p>
  
  <p>Please help </p>
  
  <pre><code>        JButton submit = new JButton("Login");                  //Directly added to the frame
          JLabel head1 = new JLabel("LOGIN SCREEN");              //Component of head panel
          JLabel userName = new JLabel("Username ");              //Component of body panel 1
          JTextField userNameField = new JTextField(15);          //Component of body panel 1
          JLabel userNameExtra = new JLabel(" *");                //Component of body panel 1
          userNameExtra.setForeground(Color.red);
          JLabel pass = new JLabel("Password ");                  //Component of body panel 2
          JPasswordField passField = new JPasswordField(15);              //Component of body panel 2
          JLabel passExtra = new JLabel(" *");                    //Component of body panel 2
          passExtra.setForeground(Color.red);
          JLabel footer = new JLabel ("*This field is mandatory");
          JLabel credits = new JLabel("-by Nikhil");
  
          //---Preparing the panels with their components---
          headPanel.setLayout(new BoxLayout(headPanel,BoxLayout.Y_AXIS));
          headPanel.add(head1);
          //userNamePanel.setLayout(new BoxLayout(userNamePanel,BoxLayout.X_AXIS));
          userNamePanel.add(userName);
          userName.requestFocus();
          userNamePanel.add(userNameField);
          userNamePanel.add(userNameExtra);
          //passPanel.setLayout(new BoxLayout(passPanel,BoxLayout.X_AXIS));
          passPanel.add(pass);
          passPanel.add(passField);
          passPanel.add(passExtra);
          bodyPanel.setLayout(new BoxLayout(bodyPanel,BoxLayout.Y_AXIS));
          bodyPanel.add(userNamePanel);
          bodyPanel.add(passPanel);
          bodyPanel.add(submit);
          footerPanel.setLayout(new BoxLayout(footerPanel,BoxLayout.Y_AXIS));
          footerPanel.add(footer);
  
          //---Preparing the parent panel with its components (other panels)---
          parentPanel.setLayout(new BoxLayout(parentPanel,BoxLayout.Y_AXIS));
          parentPanel.add(headPanel);
          parentPanel.add(bodyPanel);
          parentPanel.add(footerPanel);
  
          //---Putting the parent panel in the frame---
          frame.setTitle("Login GUI Application");
          frame.getContentPane().add(parentPanel,"Center");
          frame.getContentPane().add(credits,"South");
          frame.setSize(500,500);
          frame.setVisible(true);
      }
  }
  </code></pre>