I have made my code shorter to try and just show the error. I am using 4 panels in a container and i have coloured each panel to show they are there. My problem is my left panel which is set to LINE_START in the container. This panel's layout is set to GridBagLayout. I am trying to create a registration type panel. I am starting off with my registration title, i have set the gridx and gridy both too 0 but my title is being put in the middle of the panel. I cant seem to get it too work from the top of the panel. Please if you could look at the code and see why this is happening. The code should compile if you like to take a look.
Thanks very much.
CODE
import java.awt.*; //the importing of required libraries
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Album extends JFrame
{
private JPanel topPanel, leftPanel, centerPanel, rightPanel, bottomPanel;
private JLabel regUser, regPassword, regName, regTitle;
private JTextField regUser2, regPassword2, regName2;
private String regUser3, regPassword3, regName3;
private Container cPane;
public Album()
{
super();
createGUI();
regUser3 = "";
regPassword3 ="";
regName3 ="";
}
private void createGUI(){
cPane = getContentPane();
arrangeCompenents();
setTitle("Main");
setLocation(new Point(0, 0));
setVisible(true);
setSize(800, 608);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
private void arrangeCompenents(){
topPanel = new JPanel();
leftPanel = new JPanel();
centerPanel = new JPanel();
rightPanel = new JPanel();
bottomPanel = new JPanel();
regUser = new JLabel("Username");
regPassword = new JLabel("Password");
regName = new JLabel("Full Name");
regTitle = new JLabel("REGISTER");
regUser2 = new JTextField(10);
regPassword2 = new JTextField(10);
regName2 = new JTextField(10);
regTitle.setFont(regTitle.getFont().deriveFont(Font.BOLD, 12));
regTitle.setForeground(new Color(0, 153, 254));
cPane.setLayout(new BorderLayout());
cPane.add(topPanel,BorderLayout.PAGE_START);
cPane.add(centerPanel,BorderLayout.CENTER);
cPane.add(bottomPanel, BorderLayout.PAGE_END);
cPane.add(leftPanel, BorderLayout.LINE_START);
topPanel.setBackground(Color.black);
centerPanel.setBackground(Color.green);
bottomPanel.setBackground(Color.blue);
leftPanel.setBackground(Color.white);
leftPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc01 = new GridBagConstraints();
gbc01.gridx = 0;
gbc01.gridy = 0;
gbc01.insets = new Insets(0, 0, 5, 5);
gbc01.anchor = GridBagConstraints.PAGE_END;
leftPanel.add(regTitle, gbc01);
GridBagConstraints gbc02 = new GridBagConstraints();
gbc02.gridx = 0;
gbc02.gridy = 1;
gbc02.insets = new Insets(5, 5, 5, 5);
leftPanel.add(regUser, gbc02);
GridBagConstraints gbc03 = new GridBagConstraints();
gbc03.gridx = 1;
gbc03.insets = new Insets(5, 5, 5, 5);
gbc03.gridy = 1;
gbc03.gridwidth = 2;
gbc03.fill = GridBagConstraints.BOTH;
leftPanel.add(regUser2, gbc03);
GridBagConstraints gbc04 = new GridBagConstraints();
gbc04.gridx = 0;
gbc04.gridy = 2;
gbc04.insets = new Insets(5, 5, 5, 5);
leftPanel.add(regPassword, gbc04);
GridBagConstraints gbc05 = new GridBagConstraints();
gbc05.gridx = 2;
gbc05.insets = new Insets(5, 5, 5, 5);
gbc05.gridy = 2;
gbc05.gridwidth = 2;
gbc05.fill = GridBagConstraints.BOTH;
leftPanel.add(regPassword2, gbc05);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Album();
}
});
}
}