所以我通过以下方式为 java 组件设置鼠标监听器:
int[] monsters = new int[12];
monsters[0] = MonsterSelector.BL_BLUE_BUFF;
monsters[1] = MonsterSelector.BL_RED_BUFF;
monsters[2] = MonsterSelector.BL_WOLVES;
monsters[3] = MonsterSelector.BL_WRAITHS;
monsters[4] = MonsterSelector.BL_GOLEMS;
monsters[5] = MonsterSelector.BARON;
monsters[6] = MonsterSelector.PU_BLUE_BUFF;
monsters[7] = MonsterSelector.PU_RED_BUFF;
monsters[8] = MonsterSelector.PU_WOLVES;
monsters[9] = MonsterSelector.PU_WRAITHS;
monsters[10] = MonsterSelector.PU_GOLEMS;
monsters[11] = MonsterSelector.DRAGON;
for(int monster = 1; monster < MonsterSelector.LAST+1; monster++){
final int currentMonster = monsters[monster-1];
ImageView iv = new ImageView(images.get(currentMonster), 50, 50);
ivs.put(currentMonster, iv);
ivs.get(currentMonster).addMouseListener(this);
}
我在我的类(class)中有以下 mouselistener 实现:
@Override
public void mouseReleased(MouseEvent e) {
Component src = (Component) e.getSource();
Log.e(TAG, ""+src.getName());
}
现在的问题是只有第一个鼠标监听器集真正给出了输出。其余的只是忽略。我究竟做错了什么?我无法在多个对象上设置相同的鼠标监听器?
最佳答案
我猜想 MonsterSelector.LAST
没有设置为应有的最高值。或者您正在初始化 MonsterSelector.BL_BLUE_BUFF
、MonsterSelector.BL_RED_BUFF
等以使其具有相同的 int 值?
关于java - 鼠标监听器只有一个实例有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12150590/