我在为其中包含车辆对象的陈列室创建GUI时遇到问题。我正在使用JFrame
并且已经获得了基本轮廓,其中包含下一个,前一个和按边缘的按钮(使用指南针布局)。我已将actionListeners
添加到其中,但我无法确定如何在Vehicle
或Showroom
类中调用相应的方法。我应该在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;
}
}
答案 0 :(得分:1)
我使用了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方法,因此您应该能够与其他类一起运行它。 祝你好运!
答案 1 :(得分:0)
你的主要应该是这样的
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new VehicleJFrame();
}
}):
}
注意:你应该像这样调用你的JFrame,因为它们不是线程安全的