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.
Here is the code
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.
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);
}
}