我试图通过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);
}
}
ItemPanel:
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
是实现您在ActionListener
方法中创建的setupCancelItemButton
的匿名类:
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);
this
实际上是指ActionListener
而不是ItemPanel
。要更正您的代码,您应该编写
ItemPanelEvent ev = new ItemPanelEvent(ItemPanel.this);
相反,它应该可以完美运行。