我试图将一些本地数据成员(在ItemPanel中)的值传递给另一个类(MainFrame),以便在MainFrame中调用一个函数。为此,我使用了Event和EventListener。我收到一个空指针异常,我不确定为什么。任何帮助将不胜感激。谢谢。
ItemPanel:
public class ItemPanel extends JPanel {
private boolean logged;
private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;
private ItemPanelLogListener itemPanelLogListener;
private JButton logBtn;
public ItemPanel(boolean logged, String item, String buyPrice, String sellPrice,
String quantity, String pcBuyPrice, String pcSellPrice) {
this.logged = logged;
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);
dim.width = 900;
setMinimumSize(dim);
logBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ItemPanelLogEvent ev = new ItemPanelLogEvent(this, getLogged(), getItem(), getBuyPrice(),
getSellPrice(), getQuantity(), getPCBuyPrice(), getPCSellPrice());
if (itemPanelLogListener != null) {
itemPanelLogListener.ItemPanelLogEventOccurred(ev);
}
System.out.println("Logged.");
// Remove after Logged
Container greatgrandparent = cancelBtn.getParent().getParent().getParent();
Container grandparent = cancelBtn.getParent().getParent();
greatgrandparent.remove(grandparent);
System.out.println("Removed due to ItemPanel being logged.");
greatgrandparent.revalidate();
greatgrandparent.repaint();
}
});
}
}
ItemPanelLogListener:
import java.util.EventListener;
public interface ItemPanelLogListener extends EventListener{
public void ItemPanelLogEventOccurred(ItemPanelLogEvent e);
}
ItemPanelLogEvent:
import java.util.EventObject;
public class ItemPanelLogEvent extends EventObject{
private boolean logged;
private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;
public ItemPanelLogEvent(Object source) {
super(source);
}
public ItemPanelLogEvent(Object source, boolean logged, String item, String buyPrice,
String sellPrice, String quantity, String pcBuyPrice,
String pcSellPrice) {
super(source);
this.logged = logged;
this.item = item;
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
this.quantity = quantity;
this.pcBuyPrice = pcBuyPrice;
this.pcSellPrice = pcSellPrice;
}
public boolean getLogged() {
return logged;
}
public String getItem() {
return item;
}
public String getBuyPrice() {
return buyPrice;
}
public String getSellPrice() {
return sellPrice;
}
public String getQuantity() {
return quantity;
}
public String getPcBuyPrice() {
return pcBuyPrice;
}
public String getPcSellPrice() {
return pcSellPrice;
}
}
大型机:
public class MainFrame extends JFrame {
private ToolBar toolBar;
private FlipFormPanel flipFormPanel;
private LogFormPanel logFormPanel;
private FlipPanel flipPanel;
private LogPanel logPanel;
private ItemPanel itemPanel;
public MainFrame() {
super("Flipping Guidance");
setSize(1258, 684);
Dimension dim = new Dimension();
dim.width = 428;
dim.height = 428;
setMinimumSize(dim);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMenuBar());
toolBar = new ToolBar();
flipFormPanel = new FlipFormPanel();
logFormPanel = new LogFormPanel();
flipPanel = new FlipPanel();
logPanel = new LogPanel();
// Item Panel Log Button
itemPanel.setItemPanelLogListener(new ItemPanelLogListener() {
public void ItemPanelLogEventOccurred(ItemPanelLogEvent e) {
boolean logged = e.getLogged();
String item = e.getItem();
String buyPrice = e.getBuyPrice();
String sellPrice = e.getSellPrice();
String quantity = e.getQuantity();
String pcBuyPrice = e.getPcBuyPrice();
String pcSellPrice = e.getPcSellPrice();
log(logged, item, buyPrice, sellPrice, quantity,
pcBuyPrice, pcSellPrice);
}
});
//Cancel
}
答案 0 :(得分:2)
在设置侦听器之前,您忘记初始化itemPanel
:
<强> MainFrame.java 强>
itemPanel = new ItemPanel(....);
itemPanel.setItemPanelLogListener(new ItemPanelLogListener() {
....
}