简体   繁体   English

如果我使用mouselistener具有标签的数组列表,那么单击标签时如何获得索引?

[英]If I have an arraylist of labels using mouselistener, how do I get an index when a label is clicked?

I'm making a card game. 我正在打纸牌游戏。 It has an arraylist of Jlabels corresponding to each card the player has. 它具有一个Jlabels数组列表,对应于玩家拥有的每张卡。

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时,如何获取单个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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过mouselistener获取Arraylist的索引? - How to get the index of an Arraylist Through mouselistener? 如何让 JTable 上的 MouseListener 工作 - How do I get the MouseListener on a JTable working 如何使用 MouseListener 单击 JLabel? - How do I click a JLabel using MouseListener? 如何让我的 MouseListener 方法在 Java 中工作? - How do I get my MouseListener method to work in Java? 如何使用MouseListener启动后台线程? - How do I need to start a background thread using MouseListener? 当它们相同时,如何在ArrayList中找到多个对象的索引 - How do I find the index of multiple objects in an ArrayList when they are identitcal 如何使用mouselistener更改面板? - How do I change a panel with mouselistener? 如何仅使用对象的属性获取ArrayList中对象的索引? - How do I get the index of an object in an ArrayList using only a property of the object? 如何使用ArrayList访问元素的特定索引? - How do i access a specific index of an element using ArrayList? 我有一个数组列表,它的大小为 1,但是当我到达索引 0 时,我得到 = java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 - I have an arraylist and its size is 1, but when i get at index 0, i get = java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM