我正在做一个纸牌游戏。它有一个 Jlabels 的数组列表,对应于玩家拥有的每张牌。
单击卡片 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);
}
}