Hi,

I need to create this Carpark application that looks like this

http://i35.photobucket.com/albums/d171/rossmc/java-1.jpg

I have got the buttons to work but when I try to make the grid it is not showing with the buttons when I run it.

This is the code I have so far. Does anyone know were I am going wrong?

import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.LineBorder;

public class Carpark extends JPanel
{
    Carpark()
    {
        
    JPanel Buttons = new JPanel();
    this.add(Buttons);
    this.setLayout(new BorderLayout());									
    this.add(Buttons,BorderLayout.NORTH);
    

    
    
    JButton Car = new JButton("Add Car");
    JButton Lorry = new JButton("Add Lorry");
    JButton Coach = new JButton("Add Coach");
    JButton Clear = new JButton("Clear All");
    JButton Save = new JButton("Save");
    JButton Load = new JButton("Load");
    JButton Current = new JButton("Current Total");
    JButton Day = new JButton("Total For Day");

    Buttons.add(Car);
    Buttons.add(Lorry);
    Buttons.add(Coach);
    Buttons.add(Clear);
    Buttons.add(Save);
    Buttons.add(Load);
    Buttons.add(Current);
    Buttons.add(Day);

    JPanel Carpark = new JPanel();
    this.add(Carpark);
    setLayout(new GridLayout(3,2));
    this.setLayout(new BorderLayout());
    this.add(Buttons,BorderLayout.CENTER);

    JLabel first = new JLabel();
    JLabel second = new JLabel();
    JLabel third = new JLabel();
    JLabel fourth = new JLabel();
    JLabel fifth = new JLabel();

    Carpark.add(first);
    Carpark.add(second);
    Carpark.add(third);
    Carpark.add(fourth);
    Carpark.add(fifth);
    }

 }

Well, for one, I believe you meant

Carpark.setLayout(new GridLayout(3,2));

rather than just

setLayout(new GridLayout(3,2));

as that is the same as doing

this.setLayout(new GridLayout(3,2));

would would be useless since you do

this.setLayout(new BorderLayout());

right after that.

Also, you probably want to do

this.add(Carpark);

after doing

this.setLayout(new BorderLayout());

rather than before, and CENTER is the default in a BorderLayout, so that is where Carpark is after doing

this.setLayout(new BorderLayout());

which means that this line

this.add(Buttons,BorderLayout.CENTER);

covers it up. You probably want to do either

this.add(Buttons,BorderLayout.NORTH);

(or is that TOP, I don't remember, off-the-top-of-my-head, see the API docs) or

this.add(Buttons,BorderLayout.SOUTH);

(or is that BOTTOM, I dont remember, off--the-top-of-my-head, see the API docs).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.