我可以向JLabel添加动作侦听器吗? [英] Can I add an action listener to a JLabel?

查看:673
本文介绍了我可以向JLabel添加动作侦听器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JLabel替换JButton,我希望我的代码在单击JLabel时执行一些操作。

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

当我使用JButton时我使用了动作监听器处理按钮的点击:

When I had the JButton I used action listener to handle clicks on the button:

myButton.addActionListener(new clicksListener(arg1,this))

当我用 myLabel替换 myButton 我在Eclipse中收到以下错误消息:

When I replaced myButton by myLabel I got the following error message in the Eclipse:


方法
addActionListener(ChipsListener)是
未定义类型JLabel

The method addActionListener(ChipsListener) is undefined for the type JLabel

但我知道应该可以将单击处理程序附加到JLabel。有人知道怎么做吗?

But I do know that it should be possible to attach a click handler to the JLabel. Does anybody know how it can be done?

推荐答案

添加 MouseListener JLabel

Add a MouseListener to the JLabel.

因为 JLabel 是一个组件,你可以添加 MouseListener 。使用该界面并在 MouseListener 上编写 mouseClicked 事件来处理点击。

Because JLabel is a Component, you can add MouseListeners to it. Use that interface and write the mouseClicked event on your MouseListener to handle the click.

这篇关于我可以向JLabel添加动作侦听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆