简体   繁体   中英

How to know on which Label I clicked [Java]

I tried .equals() and == , but nothing help. All labels I storage in ArrayList of my own class, which have JLabel.

How can I get the index of the label in ArrayList or something else?

Can my problem in using ArrayList? MouseListener


  private static MouseListener clicklvl1=new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                for (int i=0;i<shahtars.size();i+=1){
                     if (e.getSource()==shahtars.get(i).uiShahtar){
                         IDofClickedObject=i;
                    } 
                }
                if (IDofClickedObject!=-1){
                    if (counter == 0) {
                        shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1(Clicked).png"));
                        counter = 1;
                    } else if (counter == 1) {
                        // uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
                        shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
                        counter = 0;
                    }
                }
                System.out.print("x "+shahtars.get(IDofClickedObject).uiShahtar.getX()+" y  "+shahtars.get(IDofClickedObject).uiShahtar.getY());

            }

My class


class  FillShahtar implements Cloneable {
    static JLabel uiShahtar;
    static int energy;
    static double power;
    static double speed;
    String name;

And the last

 FillShahtar(int chose) {
switch (chose){
    case 1:{

        plankaDown = 1;
        plankaUp = 11;

        int xRand = (int) ( 0+Math.random()*1000);
        int yRand =(int) (0+Math.random()*600);
         int counter=0;

        /////////////////////////debug/////////////////
        System.out.print(xRand+"   "+yRand+"\n");
        //////////////////////////////////////////////

        energy = (int) (plankaDown + Math.random() * plankaUp  );
        power = (int) (plankaDown + Math.random() * plankaUp  );
        speed = (int) (plankaDown + Math.random() * plankaUp  );


        uiShahtar = new JLabel();

        uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
        uiShahtar.setLayout(new FlowLayout());

        uiShahtar.setSize(50,50);
        uiShahtar.setLocation(xRand,yRand);

        uiShahtar.setVisible(true);
        uiShahtar.addMouseListener(clicklvl1);



//        mainPanel.add(shahtars.get(counter).uiShahtar);
        counter+=1;

        break;
    }

Clicked image must change the image, but change only last Label.

Since JLabel inherits from swing's Component class, you can add a MouseListener (or MouseAdapter ) to each of your clickable labels. Using this EventListener you can find the clicked label like this:

        JLabel l = new JLabel();
        l.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                JLabel clickedLabel = (JLabel) e.getComponent();
            }
        });

In order to get the index of the clicked label within the ArrayList, use the indexOf(Object) method provided by the ArrayList:

int index = list.indexOf(clickedLabel);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM