我可以向JLabel添加动作侦听器吗? [英] Can I add an action listener to a 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替换
我在Eclipse中收到以下错误消息: myButton
时
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 MouseListener
s to it. Use that interface and write the mouseClicked
event on your MouseListener
to handle the click.
这篇关于我可以向JLabel添加动作侦听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!