0

Im looking a little bit of help here.

I have been given the task of creating a car park management system with a GUI interface as opposed to a console interface. The system has 15 spaces - 5 of those spaces being large can only fit 'large' vehicles - the other 10 are regular sized spaces. Whether they are large or not is determined by the parking attendant in the car park.

Upon entry to the car park, the attendant is required to fill in information about the car (reg number of vehicle, and to click a Yes or No radio button for whether or not the car is of 'high value' or not, or also if the car is a 'large vehicle')

I am currently trying to build the interface which when the program is run at first displays the car park (spaces will change from red to green when a space becomes available, and vice versa) which i have drawn using Graphics2D. (At this point i am not sure how to make this appear in a JFrame or whatever i need to use) Under the drawn car park with changing colours, i will include 3 JButtons - one to 'Add a Car', 'Remove a Car' and to 'Search for a Car.' I am unsure how to make the 'carPark' component and the afore mentioned JButtons to the window which will appear when it is run.

Consequently when i click these buttons a new window will appear displaying a form relevant to each button (add, remove and search)

What must i do to get the carpark and buttons to appear in the same window.

Here is the code i have created thus far - although it is very untidy and in pretty much one class i know whats going on and plan to split it up once i have everthing working. (I know this isnt efficient for writing code):

package carparksystem;

import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import java.awt.Color;


public class DrawCarPark extends javax.swing.JFrame
{
public DrawCarPark()
{   
    JButton addACar = new JButton("Add Car");
    JButton removeACar = new JButton("Remove Car");
    JButton searchCars = new JButton("Search Cars");


    JFrame frame = new JFrame();
    frame.add(new CarPark());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Car Park System");
    frame.setSize(435, 600);
    frame.setVisible(true);
    frame.setResizable(false);
    //frame.add(addACar);
    //frame.add(removeACar);
    //frame.add(searchCars);

    GroupLayout mainLayout = new GroupLayout(getContentPane());      //chosen to display components in group layout
    getContentPane().setLayout(mainLayout);
    mainLayout.setAutoCreateGaps(true);
    mainLayout.setAutoCreateContainerGaps(true);

    mainLayout.setHorizontalGroup(mainLayout.createSequentialGroup()
        .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(frame)
            .addGroup(mainLayout.createSequentialGroup()
                .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(addACar))
                .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(removeACar))
                .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(searchCars))))
            );

    mainLayout.setVerticalGroup(mainLayout.createSequentialGroup()
        .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(frame)
        .addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(addACar)
            .addComponent(removeACar)
            .addComponent(searchCars)))
        );

    //addCarForm();
    //removeCarForm();
    //searchCarForm();
}

private void addCarForm()
{        
    /*JPanel panel = new JPanel();
    //frame.add(panel);
    panel.setSize(450, 650);
    panel.setVisible(true);
    panel.getSize();*/

    JLabel regNumLabel = new JLabel("Registration Number:");
    JLabel highValLabel = new JLabel("High Value?");
    JLabel largeLabel = new JLabel("Large Vehicle?");

    JRadioButton btnYesHighVal = new JRadioButton("Yes", false);
    JRadioButton btnNoHighVal = new JRadioButton("No", true);
    JRadioButton btnYesLarge = new JRadioButton("Yes", false);
    JRadioButton btnNoLarge = new JRadioButton("No", true);

    ButtonGroup highVal = new ButtonGroup();        //allows just one radio button from the group to be selected
    highVal.add(btnYesHighVal);
    highVal.add(btnNoHighVal);

    ButtonGroup largeCar = new ButtonGroup();       //allows just one radio button from the group to be selected
    largeCar.add(btnYesLarge);
    largeCar.add(btnNoLarge);

    JTextField regNumField = new JTextField();

    JButton addCar = new JButton("   Add  ");
    JButton addCancel = new JButton("Cancel");

    GroupLayout addLayout = new GroupLayout(getContentPane());      //chosen to display components in group layout
    getContentPane().setLayout(addLayout);
    addLayout.setAutoCreateGaps(true);
    addLayout.setAutoCreateContainerGaps(true);

    addLayout.setHorizontalGroup(addLayout.createSequentialGroup()
        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumLabel)
            .addComponent(highValLabel)
            .addComponent(largeLabel))
        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumField)
            .addGroup(addLayout.createSequentialGroup()
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(btnYesHighVal)
                .addComponent(btnYesLarge))
            .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(btnNoHighVal)
                .addComponent(btnNoLarge))))
        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(addCar)
            .addComponent(addCancel))
    );

    addLayout.setVerticalGroup(addLayout.createSequentialGroup()
        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(regNumLabel)
            .addComponent(regNumField)
            .addComponent(addCar))
        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(addLayout.createSequentialGroup()
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(highValLabel)
                    .addComponent(btnYesHighVal)
                    .addComponent(btnNoHighVal))
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(largeLabel)    
                    .addComponent(btnYesLarge)
                    .addComponent(btnNoLarge)))
                .addComponent(addCancel))
        );

    setSize(375, 150);
    setTitle("Add Car");
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

private void removeCarForm()
{
    JLabel regNumLabel = new JLabel("Registration Number:");
    JTextField regNumField = new JTextField();
    JButton removeCar = new JButton("Remove");
    JButton removeCancel = new JButton("Cancel");

    GroupLayout removeLayout = new GroupLayout(getContentPane());
    getContentPane().setLayout(removeLayout);
    removeLayout.setAutoCreateGaps(true);
    removeLayout.setAutoCreateContainerGaps(true);

    removeLayout.setHorizontalGroup(removeLayout.createSequentialGroup()
        .addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumLabel))
        .addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumField))
        .addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(removeCar)
            .addComponent(removeCancel))
    );

    removeLayout.setVerticalGroup(removeLayout.createSequentialGroup()
        .addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(regNumLabel)
            .addComponent(regNumField)
            .addComponent(removeCar))
        .addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(removeCancel))
    );                

    setSize(375, 150);
    setTitle("Search Car");
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

private void searchCarForm()
{
    JLabel regNumLabel = new JLabel("Registration Number:");
    JTextField regNumField = new JTextField();
    JButton searchCar = new JButton("Search");
    JButton searchCancel = new JButton("Cancel");

    GroupLayout searchLayout = new GroupLayout(getContentPane());
    getContentPane().setLayout(searchLayout);
    searchLayout.setAutoCreateGaps(true);
    searchLayout.setAutoCreateContainerGaps(true);

    searchLayout.setHorizontalGroup(searchLayout.createSequentialGroup()
        .addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumLabel))
        .addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(regNumField))
        .addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(searchCar)
            .addComponent(searchCancel))
    );

    searchLayout.setVerticalGroup(searchLayout.createSequentialGroup()
        .addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(regNumLabel)
            .addComponent(regNumField)
            .addComponent(searchCar))
        .addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(searchCancel))
    );                

    setSize(375, 150);
    setTitle("Remove Car");
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

public static void main(String[]args)
{
    DrawCarPark carPark = new DrawCarPark();
    carPark.setVisible(true);
}
}

Also here is my main menu class with the carPark i have drawn:

package carparksystem;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;

public class CarPark extends javax.swing.JPanel
{
private void draw(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    //Draw carpark boundary
    g2.drawLine(20, 20, 400, 20);
    g2.drawLine(20, 450, 20, 20);
    g2.drawLine(20, 450, 400, 450);
    g2.drawLine(400, 20, 400, 140);
    g2.drawLine(400, 330, 400, 450);

    g2.drawString("ENTER", 370, 180);           //labels
    g2.drawString("EXIT", 375, 300);

    g2.setColor(Color.WHITE);                   //attendants box
    g2.fill3DRect(330, 200, 70, 70, true);   

    g2.setColor(Color.BLACK);                   
    g2.drawString("Attendant", 338, 230);
    g2.drawString("Station", 345, 245);
    g2.drawRect(330, 200, 70, 70);

    g2.setColor(Color.GREEN);
    g2.fillRect(40, 40, 50, 100);       //8         first row spaces
    g2.fillRect(110, 40, 50, 100);      //7
    g2.fillRect(180, 40, 50, 100);      //6
    g2.fillRect(250, 60, 40, 80);       //2
    g2.fillRect(310, 60, 40, 80);       //1

    g2.setColor(Color.BLACK);           //drawRect palces a black border around shape
    g2.drawRect(40, 40, 50, 100);   
    g2.drawRect(110, 40, 50, 100);  
    g2.drawRect(180, 40, 50, 100);    
    g2.drawRect(250, 60, 40, 80);   
    g2.drawRect(310, 60, 40, 80);

    g2.setColor(Color.GREEN);                       //second row spaces
    g2.fillRect(90, 195, 40, 80);       //13
    g2.fillRect(150, 195, 40, 80);      //12
    g2.fillRect(210, 195, 40, 80);      //11
    g2.fillRect(270, 195, 40, 80);      //3

    g2.setColor(Color.BLACK);
    g2.drawRect(90, 195, 40, 80);  
    g2.drawRect(150, 195, 40, 80);
    g2.drawRect(210, 195, 40, 80);
    g2.drawRect(270, 195, 40, 80);

    g2.setColor(Color.GREEN);                       //3rd row spaces
    g2.fillRect(30, 330, 40, 80);       //15
    g2.fillRect(90, 330, 40, 80);      //14
    g2.fillRect(150, 330, 50, 100);     //10
    g2.fillRect(220, 330, 50, 100);     //9
    g2.fillRect(290, 330, 40, 80);      //5
    g2.fillRect(350, 330, 40, 80);      //4

    g2.setColor(Color.BLACK);
    g2.drawRect(30, 330, 40, 80);
    g2.drawRect(90, 330, 40, 80);
    g2.drawRect(150, 330, 50, 100);
    g2.drawRect(220, 330, 50, 100);
    g2.drawRect(290, 330, 40, 80);
    g2.drawRect(350, 330, 40, 80);

    g2.drawString("1", 328, 105);       //place labels on each shape
    g2.drawString("2", 268, 105);
    g2.drawString("3", 288, 240);
    g2.drawString("4", 368, 375);
    g2.drawString("5", 308, 375);
    g2.drawString("6", 203, 95);
    g2.drawString("7", 133, 95);
    g2.drawString("8", 63, 95);
    g2.drawString("9", 242, 385);
    g2.drawString("10", 168, 385);
    g2.drawString("11", 225, 240);
    g2.drawString("12", 165, 240);
    g2.drawString("13", 105, 240);
    g2.drawString("14", 103, 375);
    g2.drawString("15", 43, 375);
}

@Override
public void paintComponent(Graphics g)
{
    draw(g);
}

}

Thanks in advance if anyone is willing to help.

user3223921
  • 1
  • 1
  • 5

1 Answers1

0

The answer to the actual question you've asked is "use a layout manager in a JFrame and/or JPanel to position components, but think you already know that. You've used a GroupLayout manager in one of your two JFrames.

I think your problem may be that you have two of them. You declare a JFrame variable in DrawCarPark, and that class also extends JFrame. You probably don't need or want both, and it may be confusing you -- if you expect things to show up in the declared JFrame, the JFrame methods you call without reference to that instance will pass the compiler since DrawCarPark extends JFrame, but they won't be doing what you want. If you want one window with all your GUI elements in it, get rid of either the variable or the extension to JFrame.

I might suggest using a different layout manager for the remaining JFrame -- its default is BorderLayout, and it seems to me that might be better for what you want. Put your buttons into a JPanel (using whatever other layout manager you want, GroupLayout or whatever), and put that panel in the NORTH section of the BorderLayout. Then put your CarPark panel into the CENTER of that BorderLayout, and the resulting JFrame will have both.

Good luck with it.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thanks for your quick reply bud. Another question though, when you say 'get rid of the variable' does that mean the JFrame that i have declared called 'frame.' When i do that it messes up my groupLayout statement since i have added a JFrame into the layout. do i just have to remove it from the layout statement as well then? And what are you referring to as the 'remaining JFrame' Thanks in advance dude – user3223921 Nov 29 '14 at 14:06
  • `DrawCarPark` extends `JFrame` and is instantiated as `carPark`. There is also a `JFrame` instantiated within `DrawCarPark` as `frame`. You call group layout things on `frame`, and you also call things like `getContentPane()` without using "`frame.`"; the latter will be called on the `this` instance of DrawCarPark, which in your case will be `carPark`. If you want one window, then use either `carPark` or `frame`, not both. Whichever one you change, you will have to change the method calls that refer to the other one to have all of them refer to the same JFrame instance. – arcy Nov 29 '14 at 15:47