• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

I need help in Java(Layouts)

Status
Not open for further replies.

Dinokill

Member
Hi neogaf. I'm having a small problems in my layout of my program.

I'm using a BorderLayout for the main JFrame while using differents layouts for the different areas. The main layout of the west area consist of 3 panels; the main panel which arranges subpanels in a BoxLayout layout.

The problem is that there is a gap between the sub panels(first name and last name) but I want to erase that gap so the last name label is just under first name label. I tried to use setPreferresSize(new Dimension(width, height)) but is not reducing the gap at all between the subpanels.

mRngM6E.png

Here is the code

Code:
import javax.swing.*;

public class Survey
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame("Survey");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      SurveyPanel panel = new SurveyPanel();
      
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SurveyPanel extends JPanel
{
   private JLabel top, top2;
   private JPanel north, south, west;
   private JButton clear, submit;
   
   public SurveyPanel()
   {
      // Main layout of the key pad panel
      setLayout(new BorderLayout());
      setBackground(Color.green);
      setPreferredSize(new Dimension(600, 350));
      
      // North Panel
      north = new JPanel(new BorderLayout());
      top = new JLabel("Demographic Survey", SwingConstants.CENTER);
      top2 = new JLabel("Please complete this form. Press the submit button when complete", SwingConstants.CENTER);
      north.add(top, BorderLayout.NORTH);
      north.add(top2, BorderLayout.CENTER);
      
      // South Panel
      south = new JPanel();
      south.setLayout(new BoxLayout(south, BoxLayout.X_AXIS));
      clear = new JButton("Clear");
      submit = new JButton("Submit");
      south.add(Box.createHorizontalGlue());
      south.add(clear);
      south.add(Box.createRigidArea(new Dimension(10, 40)));
      south.add(submit);
      
      // West Panel
      west = new JPanel();
      west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
      
      // SubPanels for west(first name)
      JPanel fname = new JPanel();
      fname.setBackground(Color.green);
     
      JLabel firstName = new JLabel("First Name: ");
      JTextField first = new JTextField(10);
      fname.add(firstName);
      fname.add(first);
      
      // SubPanels for west(last name);
      JPanel lname = new JPanel();
      lname.setBackground(Color.green);
      JLabel lastName = new JLabel("Last Name: ");
      JTextField last = new JTextField(10); 
      lname.add(lastName);
      lname.add(last);
      
      west.add(fname);
      west.add(lname);      
      
      
      add(north, BorderLayout.NORTH);
      add(south, BorderLayout.SOUTH);
      add(west, BorderLayout.WEST);
      
   }
}
 
I think I got around this in college by adding panels to a panel. So I would add 2 panels of the same layout to the West panel in your example. Of course then I started using stuff like Netbeans to design the layouts by hand and copying the automatic code :) The assignment was pretty stupid back then because no one develops GUIs this way.
 
Status
Not open for further replies.
Top Bottom