I'm making a card game. It has an arraylist of Jlabels corresponding to each card the player has.
When the card jlabel is clicked, how do I get the index of the individual JLabel, so I can call a playcard() method that plays the card using the given index?
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);
}
}
It seems like you want to use the indexOf()
method of ArrayList. https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.