简体   繁体   中英

Listener on text within JLabel

I'm building a program in Java, using Swing , that will act as an interactive presentation.

I have paragraphs I need to display (presumably in JLabel s) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.

I know there must be a way to accomplish this...

If you want to apply some style, you can use HTML in your JLabel 's content.

If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel , you need your component to be split in a more detailed way. Create a container( JPanel ) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.

In this way, every smart part of text will be a standalone component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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