0

我在为其中包含车辆对象的陈列室创建 GUI 时遇到问题。我正在使用 aJFrame并且已经获得了带有下一个、上一个和边缘销售按钮的基本轮廓(使用 Compass 布局)。我已经添加actionListeners到其中的每一个,但我无法弄清楚如何在VehicleorShowroom类中调用相应的方法。我应该在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;
        }
    }
4

2 回答 2

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 方法,因此您应该能够与其他类一起运行它。祝你好运!

于 2013-11-13T14:18:20.767 回答
0

你的主要应该是这样的

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new VehicleJFrame();
        }
    }):
}

注意:您应该像这样调用您的 JFrame,因为它们不是线程安全的

于 2013-11-13T14:19:37.353 回答