0

我正在做一个纸牌游戏。它有一个 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);
        }
    }
4

1 回答 1

2

看来您想使用indexOf()ArrayList 的方法。 https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)

于 2019-09-14T04:31:39.580 回答