gpt4 book ai didi

java - 为车辆陈列室实现 GUI

转载 作者:行者123 更新时间:2023-12-01 13:51:01 24 4
gpt4 key购买 nike

我在为其中包含车辆对象的陈列室创建 GUI 时遇到问题。我正在使用 JFrame 并获得了基本轮廓,其中包含下一个、上一个按钮以及边缘的销售按钮(使用 Compass 布局)。我已向其中每个添加了 actionListeners,但我不知道如何调用 VehicleShowroom 类中的相应方法。我应该在 JFrame 类中有一个 main 方法吗?

我还需要在中央面板中显示当前车辆的详细信息,但不明白如何做到这一点?

下面是一些代码:

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

最佳答案

我使用了 JOptionPane 并进行了一些细微的更改来演示在框架中显示数据的方式。我添加了一个 JTextPane 来显示数据,以及一种通过您制作的 GUI 中的按钮添加汽车的方法。要从这里继续,我建议在添加更多汽车后,使用上一个和下一个按钮来迭代陈列室中的汽车数组列表。看看是否有帮助:

    public class VehicleJFrame extends JFrame implements ActionListener
{

private JButton previousButton;
private JButton addButton; //NEW - For adding vehicles
private JButton sellButton;
private JButton nextButton;
private JPanel centerPanel;
private JTextPane displayPane;
private Showroom theShowRoom;


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");
addButton = new JButton("Add");
sellButton.addActionListener(this);
nextButton = new JButton("Next ");
nextButton.addActionListener(this);
addButton.addActionListener(this);
displayPane = new JTextPane();//NEW - For displaying
displayPane.setEditable(false);//NEW - Only for display so no editing
theShowRoom = new Showroom("Showroom 1");//NEW - To store our vehicles in

getContentPane().setLayout(new BorderLayout(10, 10));
getContentPane().add(displayPane, BorderLayout.CENTER);//NEW - The place to display cars in the middle
getContentPane().add(previousButton, BorderLayout.WEST);
getContentPane().add(sellButton, BorderLayout.SOUTH);
getContentPane().add(addButton, BorderLayout.NORTH);//NEW - The add button
getContentPane().add(nextButton, BorderLayout.EAST);

}


@Override
public void actionPerformed(ActionEvent e)
{

if (e.getSource() == previousButton)
{

}
else if (e.getSource() == sellButton)
{

}
else if (e.getSource() == nextButton)
{

}
else if (e.getSource() == addButton){
Vehicle v = new Vehicle(JOptionPane.showInputDialog(null, "Manafacturer"),
JOptionPane.showInputDialog(null, "Model"),
JOptionPane.showInputDialog(null, "Vin"),
JOptionPane.showInputDialog(null, "Manafacture date"),
JOptionPane.showInputDialog(null, "Tax band").charAt(0),
Integer.valueOf(JOptionPane.showInputDialog(null, "cost")));
theShowRoom.addVehicle(v);//Add to showroom
displayPane.setText(theShowRoom.getCurrentVehicle().toString());
}



}

public static void main(String[] args){ //To test the program
javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new VehicleJFrame();

}
});
}}

我添加了一个 main 方法,因此您应该能够与其他类一起运行它。祝你好运!

关于java - 为车辆陈列室实现 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19952965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com