Java 在多个JLabel上侦听鼠标

Java 在多个JLabel上侦听鼠标,java,swing,jlabel,mouselistener,Java,Swing,Jlabel,Mouselistener,我已经创建了一些动态的jlabel,并在每个标签上添加了MouseLister。现在的问题是如何检测我单击了哪个JLabel? 这是我的密码 int c1=40; setLayout(null); jPanel1.setSize(new Dimension(500, 200)); jPanel1.setLayout(new GridLayout(4, 10)); JLabel[] jl = new JLabel[c1]; for(int i

我已经创建了一些动态的
jlabel
,并在每个标签上添加了
MouseLister
。现在的问题是如何检测我单击了哪个
JLabel
? 这是我的密码

    int c1=40;
    setLayout(null);
    jPanel1.setSize(new Dimension(500, 200));
    jPanel1.setLayout(new GridLayout(4, 10));
        JLabel[] jl = new JLabel[c1];
    for(int i=c1-1; i>=0; i--){
        jl[i] = new JLabel();
        //jl.setText("O");
        jl[i].setPreferredSize(new Dimension(20,20));
        jl[i].setIcon(new ImageIcon(NewJFrame.class.getResource("booked.png")));
        jl[i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jPanel1.add(jl[i]);
        jl[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me){

            }
    });
    }

对于每个JLabel,您将添加一个新的/单独的MouseAdapter对象

 jl[i].addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me){

        }
});
因此,在mousePressed事件中调用me.getComponent()应该会返回Label对象

jl[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me){
                   //Better to check if its returning JLabel obejct using instance of
                   JLabel c = (JLabel) me.getComponent();
            }
    });

对于每个JLabel,您将添加一个新的/单独的MouseAdapter对象

 jl[i].addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me){

        }
});
因此,在mousePressed事件中调用me.getComponent()应该会返回Label对象

jl[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me){
                   //Better to check if its returning JLabel obejct using instance of
                   JLabel c = (JLabel) me.getComponent();
            }
    });

对于每个JLabel,您将添加一个新的/单独的MouseAdapter对象

 jl[i].addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me){

        }
});
因此,在mousePressed事件中调用me.getComponent()应该会返回Label对象

jl[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me){
                   //Better to check if its returning JLabel obejct using instance of
                   JLabel c = (JLabel) me.getComponent();
            }
    });

对于每个JLabel,您将添加一个新的/单独的MouseAdapter对象

 jl[i].addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me){

        }
});
因此,在mousePressed事件中调用me.getComponent()应该会返回Label对象

jl[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me){
                   //Better to check if its returning JLabel obejct using instance of
                   JLabel c = (JLabel) me.getComponent();
            }
    });

但是我怎样才能唯一地识别那个JLabel呢@Sarfaraz KhanOK我得到了解决方案,我为每个JLabel添加了名称,并使用me.getComponent().getName()可以识别每个JLabel。谢谢你的帮助。但是我怎样才能唯一地识别那个JLabel呢@Sarfaraz KhanOK我得到了解决方案,我为每个JLabel添加了名称,并使用me.getComponent().getName()可以识别每个JLabel。谢谢你的帮助。但是我怎样才能唯一地识别那个JLabel呢@Sarfaraz KhanOK我得到了解决方案,我为每个JLabel添加了名称,并使用me.getComponent().getName()可以识别每个JLabel。谢谢你的帮助。但是我怎样才能唯一地识别那个JLabel呢@Sarfaraz KhanOK我得到了解决方案,我为每个JLabel添加了名称,并使用me.getComponent().getName()可以识别每个JLabel。感谢您的帮助。1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。可以将布局添加到(2)以获得更早的帮助,发布一个(最小的完全可验证的示例)或(短的、自包含的、正确的示例)。考虑使用多个<代码> JButton < /Cult>组件,使用<代码> ActuistListor < /C> >而不是标签/鼠标监听器组合。这对用户来说更好,因为他们可以使用鼠标或键盘来操作它们。1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。可以将布局添加到(2)以获得更早的帮助,发布一个(最小的完全可验证的示例)或(短的、自包含的、正确的示例)。考虑使用多个<代码> JButton < /Cult>组件,使用<代码> ActuistListor < /C> >而不是标签/鼠标监听器组合。这对用户来说更好,因为他们可以使用鼠标或键盘来操作它们。1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。可以将布局添加到(2)以获得更早的帮助,发布一个(最小的完全可验证的示例)或(短的、自包含的、正确的示例)。考虑使用多个<代码> JButton < /Cult>组件,使用<代码> ActuistListor < /C> >而不是标签/鼠标监听器组合。这对用户来说更好,因为他们可以使用鼠标或键盘来操作它们。1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。可以将布局添加到(2)以获得更早的帮助,发布一个(最小的完全可验证的示例)或(短的、自包含的、正确的示例)。考虑使用多个<代码> JButton < /Cult>组件,使用<代码> ActuistListor < /C> >而不是标签/鼠标监听器组合。这对用户来说更好,因为他们可以使用鼠标或键盘来操作它们。