Can I add an action listener to a JLabel?

后端 未结 4 1344
小鲜肉
小鲜肉 2021-02-05 09:59

I want to replace a JButton by a JLabel and I want my code to perform some action when the JLabel is clicked.

When I had the JButton I used action listener to handle cli

4条回答
  •  余生分开走
    2021-02-05 10:39

    An easier approach is to just use a JButton since it already supports this functionality by using an ActionListener.

    You can make the JButton look like a JLabel by using:

    button.setBorderPainted( false );
    

    This approach is for when you want to handle a mouseClick, since an ActionEvent is guaranteed to be generated, whereas as mouseClicked event when using a MouseListener may not be generated in all situations, which can confuse the user.

提交回复
热议问题