Java - 带GUI的停车场系统

时间:2014-11-29 13:21:38

标签: java swing user-interface

我在这里寻求一些帮助。

我的任务是创建一个带有GUI界面而不是控制台界面的停车场管理系统。该系统有15个空间 - 其中5个空间很大,只适合'大型'车辆 - 另外10个是常规空间。它们是否大,取决于停车场的停车服务员。

进入停车场后,服务员需要填写有关汽车的信息(车辆的编号,并点击“是”或“否”单选按钮,判断该车是否为“高价值” ,或者如果汽车是“大型车辆”)

我正在尝试构建界面,当程序运行时首先显示停车场(当空间可用时空间将从红色变为绿色,反之亦然)我使用Graphics2D绘制。 (此时我不知道如何使它出现在JFrame或我需要使用的任何东西)在绘制的停车场,颜色变化,我将包括3个JButtons - 一个'添加汽车','删除一辆车'和'搜索汽车'。我不确定如何将'carPark'组件和前面提到的JButton设置为运行时出现的窗口。

因此,当我点击这些按钮时,会出现一个新窗口,显示与每个按钮相关的表格(添加,删除和搜索)

我必须做些什么才能让停车场和按钮出现在同一个窗口中。

这是我到目前为止创建的代码 - 尽管它非常不整洁,而且在一个类中,我知道最近会发生什么,并计划在我工作之后将其拆分。 (我知道这对编写代码没有效率):

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);
}
}

这里是我的主菜单类,我已经绘制了carPark:

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);
}

}

如果有人愿意提供帮助,请提前致谢。

1 个答案:

答案 0 :(得分:0)

您所问的实际问题的答案是"使用JFrame和/或JPanel中的布局管理器来定位组件,但您认为已经知道了。您已在两个JFrame中使用了GroupLayout管理器。

我认为你的问题可能是你有两个。您在DrawCarPark中声明了一个JFrame变量,该类也扩展了JFrame。您可能不需要或不想要两者,这可能让您感到困惑 - 如果您希望在声明的JFrame中显示内容,则在不引用该实例的情况下调用的JFrame方法将传递编译器,因为DrawCarPark扩展了JFrame ,但他们不会做你想做的事。如果您想要一个包含所有GUI元素的窗口,请删除JFrame的变量或扩展名。

我可能会建议为剩下的JFrame使用不同的布局管理器 - 它的默认值是BorderLayout,在我看来,对你想要的东西可能更好。将您的按钮放入JPanel(使用您想要的任何其他布局管理器,GroupLayout或其他),并将该面板放在BorderLayout的NORTH部分。然后将您的CarPark面板放入BorderLayout的CENTER中,生成的JFrame将同时具有这两者。

祝你好运。