我在为其中包含车辆对象的陈列室创建 GUI 时遇到问题。我正在使用 aJFrame
并且已经获得了带有下一个、上一个和边缘销售按钮的基本轮廓(使用 Compass 布局)。我已经添加actionListeners
到其中的每一个,但我无法弄清楚如何在Vehicle
orShowroom
类中调用相应的方法。我应该在JFrame
课堂上有一个主要方法吗?
我还需要在中央面板中显示当前车辆的详细信息,但不明白我是如何做到的?
这是一些代码:
public class VehicleJFrame extends JFrame implements ActionListener
{
private JButton previousButton;
private JButton sellButton;
private JButton nextButton;
private JPanel centerPanel;
public VehicleJFrame()
{
super("Vehicle GUI");
setSize(800, 400);
setLocation(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createGUI();
setVisible(true);
}
public void createGUI()
{
previousButton = new JButton();
sellButton = new JButton();
nextButton = new JButton();
centerPanel = new JPanel();
//Add code for center panel to display the current vehicle
previousButton = new JButton("Previous");
previousButton.addActionListener(this);
sellButton = new JButton("Sell");
sellButton.addActionListener(this);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
getContentPane().setLayout(new BorderLayout(10, 10));
getContentPane().setBackground(Color.RED);
getContentPane().add(previousButton, BorderLayout.WEST);
getContentPane().add(sellButton, BorderLayout.SOUTH);
getContentPane().add(nextButton, BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == previousButton)
{
//What goes here?
}
else if (e.getSource() == sellButton)
{
}
else if (e.getSource() == nextButton)
{
}
}
}
public class Vehicle
{
//**********Declarations**********
private String manufacturer;
private String model;
private String customerName = null;
private String VIN;
private String dateOfManufacture;
private String dateOfSale = null;
private Boolean sold = false;
private char taxBand;
private int costOfVehicle;
//**********Constructor**********
public Vehicle(String man, String mod, String VIN, String dateOfMan, char taxBand, int costOfVehicle)
{
this.manufacturer = man;
this.model = mod;
this.VIN = VIN;
this.dateOfManufacture = dateOfMan;
this.taxBand = taxBand;
this.costOfVehicle = costOfVehicle;
}
//**********toString() method for Vehicle information**********
@Override
public String toString()
{
return "Vehicle\n{\n" + " manufacturer = " + manufacturer
+ "\n model = " + model
+ "\n customerName = " + customerName
+ "\n VIN = " + VIN
+ "\n dateOfManufacture = " + dateOfManufacture
+ "\n dateOfSale = " + dateOfSale
+ "\n sold = " + sold
+ "\n taxBand = " + taxBand
+ "\n CO2 Emmissions = " + getCO2Group()
+ "\n costOfVehicle = " + costOfVehicle + "\n" + '}';
}
public String buyVehicle(String customerName, String dateOfSale)
{
this.customerName = customerName;
this.dateOfSale = dateOfSale;
sold = true;
return sold.toString();
}
public class Showroom
{
private ArrayList<Vehicle> vehicleArrayList;
private String showroomName;
private int currentPos;
public Showroom(String name)
{
showroomName = name;
vehicleArrayList = new ArrayList();
currentPos = 0;
}
public boolean addVehicle(Vehicle newVehicle)
{
vehicleArrayList.add(currentPos, newVehicle);
return true;
}
public Vehicle getCurrentVehicle()
{
return vehicleArrayList.get(currentPos);
}
public boolean next()
{
if (vehicleArrayList.size() - 1 > currentPos)
{
currentPos++;
return true;
}
else
{
return false;
}
}
public boolean previous()
{
if (currentPos > 0)
{
currentPos--;
return true;
}
else
{
return false;
}
}