通过EventObject-ClassCastException同一类(Java)的getter和setter

通过EventObject-ClassCastException同一类(Java)的getter和setter,java,class,events,classcastexception,Java,Class,Events,Classcastexception,我试图通过EventObject获取和设置一个类的值,但是当将getSource()转换为适当的类时,我遇到了一个具有相同类的ClassCastException。我如何在通过EventObject获取和设置值的同时解决这个问题 谢谢,, 猎人 ItemPanelEvent: import java.util.EventObject; public class ItemPanelEvent extends EventObject implements Items{ ItemPanel ite

我试图通过EventObject获取和设置一个类的值,但是当将
getSource()
转换为适当的类时,我遇到了一个具有相同类的ClassCastException。我如何在通过EventObject获取和设置值的同时解决这个问题

谢谢,, 猎人

ItemPanelEvent:

import java.util.EventObject;

public class ItemPanelEvent extends EventObject implements Items{

ItemPanel itemPanel;

public ItemPanelEvent(Object source) {
    super(source);
}

public ItemPanelEvent(Object source, int position) {
    super(source);
    itemPanel = (ItemPanel) getSource();
    itemPanel.setPosition(position);
}

public int getPosition() {
    itemPanel = (ItemPanel) getSource();
    return itemPanel.getPosition();
}

public void setPosition(int position) {
    itemPanel = (ItemPanel) getSource();
    itemPanel.setPosition(position);
}

}
项目小组:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ItemPanel extends JPanel implements Items{

private int position;

private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;

private JLabel itemLabel;
private JLabel buyPriceLabel;
private JLabel sellPriceLabel;
private JLabel quantityLabel;
private JLabel pcBuyPriceLabel;
private JLabel pcSellPriceLabel;

private JLabel itemValue;
private JLabel buyPriceValue;
private JLabel sellPriceValue;
private JLabel quantityValue;
private JLabel pcBuyPriceValue;
private JLabel pcSellPriceValue;

private JButton logBtn;
private JButton editBtn;
private JButton cancelBtn;



private ItemPanelListener itemPanelListener;

public ItemPanel(int position, String item, String buyPrice, String sellPrice,
        String quantity, String pcBuyPrice, String pcSellPrice) {
    this.position = position;
    this.item = item;
    this.buyPrice = buyPrice;
    this.sellPrice = sellPrice;
    this.quantity = quantity;
    this.pcBuyPrice = pcBuyPrice;
    this.pcSellPrice = pcSellPrice;

    Dimension dim = getPreferredSize();
    dim.height = 100;
    setPreferredSize(dim);

    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));

    logBtn = new JButton("Log Item");
    editBtn = new JButton("Edit Item");
    cancelBtn = new JButton("Cancel Item");


    setupLabels();
    setupCancelItemButton();

    layoutComponents();


}

public int getPosition() {
    return position;
}

public void setPosition(int pos) {
    position = pos;
}

public void setupCancelItemButton() {
    cancelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            ItemPanelEvent ev = new ItemPanelEvent(this);

            if (itemPanelListener != null) {
                itemPanelListener.itemPanelEventOccurred(ev);
            }

        }
    });
}

public void setItemPanelListener(ItemPanelListener listener) {
    this.itemPanelListener = listener;
}

public void setupLabels() {
    itemLabel = new JLabel("ITEM:");
    buyPriceLabel = new JLabel("BUY PRICE:");
    sellPriceLabel = new JLabel("SELL PRICE:");
    quantityLabel = new JLabel("QUANTITY:");
    pcBuyPriceLabel = new JLabel("PC BUY PRICE:");
    pcSellPriceLabel = new JLabel("PC SELL PRICE:");

    itemValue = new JLabel(this.item);
    buyPriceValue = new JLabel(this.buyPrice);
    sellPriceValue = new JLabel(this.sellPrice);
    quantityValue = new JLabel(this.quantity);
    pcBuyPriceValue = new JLabel(this.pcBuyPrice);
    pcSellPriceValue = new JLabel(this.pcSellPrice);
}

public void layoutComponents() {
    GridBagConstraints gc = new GridBagConstraints();
    setLayout(new GridBagLayout());
    gc.fill = GridBagConstraints.NONE;

    // First Row
    gc.gridy = 0;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.gridx = 0;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(itemLabel, gc);

    gc.gridx = 1;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(itemValue, gc);

    gc.gridx = 2;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(buyPriceLabel, gc);

    gc.gridx = 3;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(buyPriceValue, gc);

    gc.gridx = 4;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(sellPriceLabel, gc);

    gc.gridx = 5;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(sellPriceValue, gc);

    gc.gridx = 6;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(quantityLabel, gc);

    gc.gridx = 7;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(quantityValue, gc);

    gc.gridx = 8;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(pcBuyPriceLabel, gc);

    gc.gridx = 9;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(pcBuyPriceValue, gc);

    gc.gridx = 10;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(pcSellPriceLabel, gc);

    gc.gridx = 11;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(pcSellPriceValue, gc);

    // Second Row
    gc.gridy = 1;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.gridx = 6;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(logBtn, gc);

    gc.gridx = 8;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(editBtn, gc);

    gc.gridx = 10;
    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(cancelBtn, gc);
}

}

ItemPanel$1
是实现您在
setupCancelItemButton
方法中创建的
ActionListener
的匿名类:

cancelBtn.addActionListener(new ActionListener() {  // This creates a new class implementing ActionListener
    public void actionPerformed(ActionEvent e) {

        ItemPanelEvent ev = new ItemPanelEvent(this);    // Here is the problem

        if (itemPanelListener != null) {
            itemPanelListener.itemPanelEventOccurred(ev);
        }

    }
});
当你写作时

ItemPanelEvent ev = new ItemPanelEvent(this);
实际上指的是
操作侦听器
而不是
项目面板

要更正代码,您应该编写

ItemPanelEvent ev = new ItemPanelEvent(ItemPanel.this);

相反,它应该可以完美地工作。

请阅读以下内容:不要盲目地对对象进行大小写,使用
instanceof
测试对象是否是您期望的类型,然后再对其进行转换。我为这个问题添加了大量的代码。感谢@MadProgrammer的想法,但我知道事件的来源必须是ItemPanel,但由于ClassCastException同一类错误,我无法转换到它。使用
instanceof
进行测试将表明我无法强制转换源代码,因此无法实现修改ItemPanel类中的值的目标。这是我在控制台中收到的错误,以防它有助于澄清问题:线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:ItemPanel$1不能在ItemPanelEvent.getPosition(ItemPanelEvent.java:18)在FlipPanel$1处强制转换为ItemPanel。ItemPanel事件在ItemPanel$1处发生(FlipPanel.java:65)。actionPerformed(ItemPanel.java:92)@HunterCorry
ItemPanel$1
表示在
ItemPanel
内的匿名类,不是
ItemPanel
本身。你能发布
项目面板
的完整代码吗?非常感谢你的帮助@PaulBoddington