我正在为自己的游戏开发广告资源UI,并且在过去的一天中,我一直在解决这个问题,我将尽我所能尽力清楚地解释问题。
我有一个for循环,可为我的库存用户界面创建行,它添加的行数与库存中物品的数量一样多。由于在任何给定时间库存中可能会有不同数量的独特商品,因此必须具有灵活性。
for (int i = 0; i < p1.getPi().getInventoryLength(); i++) {
itemID = (p1.getPi().generateItem(p1.getPi().getItemID(i)).getItemID());
///////// ITEM ICON ///////////
itemLabel = new JLabel(loadMenuIcon(p1.getPi().generateItem(p1.getPi().getItemID(i)).getIcon(), 20, 20));
gc.weightx = 0.1;
gc.weighty = (i+1 == p1.getPi().getInventoryLength()) ? 1 : 0;
gc.gridx = 0;
gc.gridy = i;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.FIRST_LINE_START;
gc.insets = new Insets(0, 0, 0, 0);
inventoryPanel.add(itemLabel, gc);
///////// ITEM NAME ///////////
name = new JTextField(p1.getPi().generateItem(p1.getPi().getItemID(i)).getItemName());
name.setBorder(null);
name.setEditable(false);
name.setHighlighter(null);
name.setFont(inGameFont.getFont());
gc.weightx = 0.7;
gc.weighty = 0;
gc.gridx = 1;
gc.gridy = i;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.FIRST_LINE_START;
gc.insets = new Insets(4, 0, 0, 0);
inventoryPanel.add(name, gc);
///////// ITEM COUNT ///////////
amount = new JTextField("x " + p1.getPi().getAmount(i));
amount.setBorder(null);
amount.setEditable(false);
amount.setHighlighter(null);
amount.setFont(inGameFont.getFont());
gc.weightx = 0.1;
gc.weighty = 0;
gc.gridx = 2;
gc.gridy = i;
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.FIRST_LINE_START;
gc.insets = new Insets(4, 0, 0, 0);
inventoryPanel.add(amount, gc);
///////// USE BUTTON ///////////
useItem = new JButton("Use");
useItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
InventoryClicks gc = new InventoryClicks(this, itemID);
if (inGameInventoryListener != null) {
inGameInventoryListener.formEventOccurred(gc);
}
}
});
useItem.setFont(inGameFont.getFont());
useItem.setHorizontalAlignment(SwingConstants.CENTER);
useItem.setPreferredSize(new Dimension(60, 18));
useItem.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY));
gc.weightx = 0.1;
gc.weighty = 0;
gc.gridx = 3;
gc.gridy = i;
gc.fill = GridBagConstraints.REMAINDER;
gc.anchor = GridBagConstraints.FIRST_LINE_END;
gc.insets = new Insets(3, 0, 0, 0);
inventoryPanel.add(useItem, gc);
但是,我面临的问题是,添加到我的按钮上的actionlistener最终在我的itemID变量中具有相同的值。这意味着无论我按下哪个按钮,我总是得到相同的最终结果。
inventoryBag.SetInGameInventoryListener(new InGameInventoryListener() {
@Override
public void formEventOccurred(InventoryClicks c) {
System.out.println(c.getID());
}
});
这是我从按下的按钮中提取信息的方式。
我已经尝试了很多不同的方法来存储itemID变量,但总是遇到相同的问题,即我无法访问正确的变量,或者设法保存变量但无法在侦听器中访问它。我尝试将itemID变量与按钮的hashcode一起存储在hashmap中
private HashMap<Integer, Integer> buttonIndex;
public HashMap<Integer, Integer> getButtonIndex() {
return buttonIndex;
}
buttonIndex.put(useItem.hashCode(), itemID);
buttonIndex = inventoryBag.getButtonIndex();
inventoryBag.SetInGameInventoryListener(new InGameInventoryListener() {
@Override
public void formEventOccurred(InventoryClicks c) {
System.out.println(buttonIndex.get(c.getID()));
}
});
如果只有我可以从清单点击的来源中获取正确的密钥,我认为哈希图解决方案将走上正确的轨道。
public class InventoryClicks extends EventObject {
private int id;
public int getID() {
return this.id;
}
public InventoryClicks(Object source, int id) {
super(source);
this.id = id;
}
我希望我设法以某种体面的方式解释了我的问题,是程序的逻辑完全消失了,还是有可能奏效?我只是挑选了一些我认为相关的代码,但是如果有什么遗漏,我可以上传更多。
我设法弄清楚了!
useItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton item = (JButton) e.getSource();
InventoryClicks gc = new InventoryClicks(this, item.hashCode());
if (inGameInventoryListener != null) {
inGameInventoryListener.formEventOccurred(gc);
}
}
});
我从e.getSource获取了正确按钮的源并将其复制到新按钮,然后从中提取hashCode。
inventoryBag.SetInGameInventoryListener(new InGameInventoryListener() {
@Override
public void formEventOccurred(InventoryClicks c) {
System.out.println(buttonIndex.get(c.getID()));
}
});
然后我可以在我的清单侦听器中提取哈希码,并使用它从我的hashmap(buttonIndex)中找到正确的ID。