简体   繁体   English

如何使JLabel可点击?

[英]How to make a JLabel clickable?

I want to customize a JLable to make him clickable, i'm not talking about adding an Action Listener because i already have, i'm talking about changing the mouse cursor's reaction when the JLabel gain Focus just like the reaction of a mouse hoover on a link in the browser. 我想自定义一个JLable让他可以点击,我不是在谈论添加一个动作监听器,因为我已经拥有,我正在谈论改变鼠标光标的反应,当JLabel获得焦点就像鼠标胡佛的反应一样浏览器中的链接。

在此输入图像描述

I have impression that is not possible with swing. 我觉得摇摆是不可能的。

怎么样:

label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

Swing APIs have a way of doing it: Swing API有一种方法:

label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

and when you change your mind: 当你改变主意时:

label.setCursor(Cursor.getDefaultCursor());

I have impression that is not possible with swing. 我觉得摇摆是不可能的。

Or maybe you just haven't yet learned how to do this perhaps? 或许你可能还没有学会如何做到这一点? A MouseListener and MouseMotionListener have worked great for me. MouseListener和MouseMotionListener对我来说非常有用。

Edit: and setting the JLabel's curser as noted by Max -- 1+ up-vote for him! 编辑:并设置JLabel的光标,如Max - 1+所述,为他投票!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM