我正在制作一款纸牌游戏。它有一个 Jlabel 数组列表,对应于玩家拥有的每张卡牌。
当单击卡片 jlabel 时,如何获取各个 JLabel 的索引,以便我可以调用使用给定索引播放卡片的 playcard() 方法?
JLabel temp = new JLabel(icon);
temp.setBounds(new Rectangle(new Point(shift, 550), temp.getPreferredSize()));
temp.addMouseListener(this);
currentdeck.add(temp);
//for loop that adds each jlabel to currentdeck
public void mousePressed(MouseEvent arg0)
{
JLabel label = (JLabel)arg0.getSource();
//int i = (how would I get the index)?
if(MouseInfo.getPointerInfo().getLocation().getX()>=label.getX()&&MouseInfo.getPointerInfo().getLocation().getY()>=label.getY())
{
UNO.playcard(int i);
}
}
最佳答案
您似乎想使用 ArrayList 的 indexOf()
方法。
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)
关于java - 如果我有一个使用 mouselistener 的标签数组列表,那么单击标签时如何获取索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932431/