&引用;javax.swing.JPanel不能被强制转换;例外,令人困惑

&引用;javax.swing.JPanel不能被强制转换;例外,令人困惑,java,image,exception,Java,Image,Exception,我试图将图像分配给网格布局,我在堆栈溢出上找到了解决方案。然而,我改变了我的代码,从那时起,现在它不工作,我得到了不能被强制转换异常,我不知道为什么 下面是引发异常的方法,特别是imagePanel行: public void setVehicle(int x, int y, Vehicle vehicle) { this.vehicles[x][y] = vehicle; ImageIcon image; if (vehicle != null) { i

我试图将图像分配给网格布局,我在堆栈溢出上找到了解决方案。然而,我改变了我的代码,从那时起,现在它不工作,我得到了不能被强制转换异常,我不知道为什么

下面是引发异常的方法,特别是imagePanel行:

public void setVehicle(int x, int y, Vehicle vehicle) {
    this.vehicles[x][y] = vehicle;
    ImageIcon image;
    if (vehicle != null) {
        image = vehicle.getImage();
    } else {
        image = null;
    }
    ((ImagePanel) this.pnlCars.getComponent(x + (y - 1) * (this.vehicles.length - 1))).setImage(image);}
这是下面的图像面板类:

public class ImagePanel extends JPanel{
private ImageIcon image;

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        // Center icon
        int width = this.getWidth();
        int height = this.getHeight();
        int iconWidth = image.getIconWidth();
        int iconHeight = image.getIconHeight();
        g.drawImage(image.getImage(), (width - iconWidth) / 2, (height - iconHeight) / 2, null);
    }
}

public void setImage(ImageIcon image) {
    this.image = image;
    this.repaint();
}
}

这将显示错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to carparkingprogram.gui.ImagePanel
    at carparkingprogram.gui.CarParkPanel.setVehicle(CarParkPanel.java:197)
    at carparkingprogram.gui.CarParkPanel$7$1.actionPerformed(CarParkPanel.java:325)
有人能帮忙吗

这是完整的代码,显然我已经尽力使它尽可能小,所以我只包括汽车部分

public static int ROWS_LARGE = 1;
public static int ROWS_SMALL = 3;
public static int ROWS = ROWS_LARGE + ROWS_SMALL;
public static int COLUMNS = 4;
public Vehicle[][] vehicles = new Vehicle[ROWS][COLUMNS];


public CarParkPanel() {
    this.setLayout(new BorderLayout());

    pnlButtons = new JPanel();
    pnlButtons.setLayout(null);
    pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.Y_AXIS));

    pnlButtons.add(btnAddCar);
    addCarMethod();

    this.add(pnlButtons);


    this.pnlLorry = new JPanel();
    this.pnlLorry.setLayout(new GridLayout(ROWS_LARGE, COLUMNS));
    this.pnlLorry.setPreferredSize(new Dimension(400, 200));

    this.pnlCars = new JPanel();
    this.pnlCars.setLayout(new GridLayout(ROWS_SMALL, COLUMNS));
    this.pnlCars.setPreferredSize(new Dimension(400, 300));

//This is the code to create the grid panel that the images are added to
    for (int row = 0; row < ROWS; row++) {
        for (int column = 0; column < COLUMNS; column++) {
            JPanel pnlVehicle = new JPanel(new BorderLayout());
            pnlVehicle.setBorder(BorderFactory.createLineBorder(Color.RED));
            pnlVehicle.addMouseListener(new MyMouseListener(column, (row * ROWS)));

            if (row < ROWS_LARGE) {
                this.pnlLorry.add(pnlVehicle);
            } else {
                this.pnlCars.add(pnlVehicle);
            }
        }
    }


    this.add(pnlButtons, BorderLayout.WEST);
    this.pnlVehicles = new JPanel(new BorderLayout());
    this.pnlVehicles.setPreferredSize(new Dimension(400, 500));
    this.pnlVehicles.add(this.pnlLorry, BorderLayout.NORTH);
    this.pnlVehicles.add(this.pnlCars, BorderLayout.AFTER_LAST_LINE);
    this.add(this.pnlVehicles, BorderLayout.EAST);


}

public int[] findEmptySpace(int start, int end) {
    for (int row = 0; row < vehicles.length; row++) {
        for (int column = start; column < end; column++) {
            //Check if space is empty
            if (this.getVehicle(row, column) == null) {
                return new int[]{
                    row,
                    column
                };
            }
        }
    }
    return null;
}


public void setVehicle(int x, int y, Vehicle vehicle) {
    this.vehicles[x][y] = vehicle;

    //TODO: Render vehicle in GUI. Check for null and remove
    ImageIcon image;
    if (vehicle != null) {
        image = vehicle.getImage();
    } else {
        image = null;
    }
    ((ImagePanel) this.pnlCars.getComponent(x + (y - 1) * (this.vehicles.length - 1))).setImage(image);

    //TODO: Update totals

}

public Vehicle getVehicle(int x, int y) {
    return this.vehicles[x][y];
}

private void addCarMethod() {
    btnAddCar.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //TODO: Create car input form.
            //REQUIREMENTS: registration, length, disabled


            final int[] value = findEmptySpace(1, vehicles.length);
            if (value != null) {
                //SHOW FORM

                final JFrame frame = new JFrame("Add Car");
                frame.setSize(350, 150);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                Labels = new GridLayout(4, 2);
                FormLayout = new GridLayout(2, 1);

                pnlForm = new JPanel();
                pnlFormButton = new JPanel();
                frame.setLayout(FormLayout);
                frame.add(pnlForm);
                frame.add(pnlFormButton);
                pnlForm.setLayout(null);

                pnlForm.setLayout(Labels);
                JLabel reg = new JLabel("Registration Number:");
                pnlForm.add(reg);

                final JTextField regTxt = new JTextField(20);
                pnlForm.add(regTxt);

                JLabel length = new JLabel("Length: ");
                pnlForm.add(length);

                final JTextField lengthTxt = new JTextField(20);
                pnlForm.add(lengthTxt);

                JLabel disBadge = new JLabel("Disabled Badge: ");
                pnlForm.add(disBadge);

                final JTextField disBadgeTxt = new JTextField(20);
                pnlForm.add(disBadgeTxt);

                JButton ok = new JButton("Ok");
                pnlFormButton.setLayout(FormLayout);
                pnlFormButton.add(ok);
                frame.setVisible(true);

                ok.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        String regNumber = regTxt.getText();
                        System.out.println("ok");
                        double length = Double.parseDouble(lengthTxt.getText());
                        boolean BadgeBoolean;

                        if (disBadgeTxt.equals("yes")) {
                            BadgeBoolean = true;
                        } else {
                            BadgeBoolean = false;
                        }

                        Car vehicle = new Car(regNumber, length, BadgeBoolean, 1);
                        System.out.println(String.valueOf(value[0]) + ":" + String.valueOf(value[1]));
                        setVehicle(value[0], value[1], vehicle);

                        frame.dispose();
                    }
                });

            } else {
                warningMethod();
            }
        }
    });
}
public static int ROWS\u LARGE=1;
公共静态int行_SMALL=3;
公共静态整数行=行\大+行\小;
公共静态int列=4;
公共车辆[][]辆=新车[行][列];
公众停车场事务委员会(){
此.setLayout(新的BorderLayout());
pnlButtons=newjpanel();
pnlButtons.setLayout(null);
pnlButtons.setLayout(新的BoxLayout(pnlButtons,BoxLayout.Y_轴));
pnlButtons.add(btnAddCar);
addCarMethod();
添加(pnlButtons);
this.pnlLorry=new JPanel();
this.pnlLorry.setLayout(新的GridLayout(行和列));
此.pnlLorry.setPreferredSize(新维度(400200));
this.pnlCars=new JPanel();
this.pnlCars.setLayout(新的GridLayout(行和列));
此.pnlCars.setPreferredSize(新尺寸(400300));
//这是创建图像添加到的网格面板的代码
对于(int row=0;rowthis.pnlCars.getComponent(x + (y - 1) * (this.vehicles.length - 1))
((ImagePanel) this.pnlCars.getComponent(x + (y - 1) * (this.vehicles.length - 1)))
      .setImage(image);}
this.pnlCars.add(pnlVehicle);