MouseListener doesn't appear to be working for me

前端 未结 3 1176
死守一世寂寞
死守一世寂寞 2021-01-15 11:20

I need to preface this with my instructor doesn\'t let us use IDE\'s. We use TextPad. I want to click on this label and it then change from \"H\" to \"T\". Currently when I

相关标签:
3条回答
  • 2021-01-15 12:00

    Your JLabel implements MouseListener, but you also need to tell the JLabel to send events to itself. At the end of the constructor you'll need to say

    addMouseListener(this);
    

    This makes more sense if you remember that you can make any class into a MouseListener, and you'd have to connect your listener to your JLabel. The fact that the JLabel is its own listener doesn't absolve you of this responsibility.

    0 讨论(0)
  • 2021-01-15 12:12

    That's because you need to add the mouse listener to your JLabel. In your constructor add:

    addMouseListener(this);
    
    0 讨论(0)
  • 2021-01-15 12:19

    You never added the MouseListener to your label.

    To do this, simply add the following code:

        addMouseListener(this);
    
    0 讨论(0)
提交回复
热议问题