突出显示链接

我正在创建一个帮助系统,它使用链接(一个JButton扩展)来扩展和折叠子面板,其中包含JLabel。 链接和可折叠面板工作,但我在实现我的查找对话框时遇到了麻烦。 我希望能够突出显示用户搜索的部分文本。 我认为我使用文本属性来强调链接中的文本是在弄乱我突出文本部分的能力,但我不确定如何以不同的方式做到这一点。 这是我的Link类的代码,我的链接子类:

public abstract class Link extends JButton { private static final int SPACE = 5; private static final Color TEXT_COLOR = Color.BLUE; public Link(String text) { super(text); setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, 2 * SPACE)); setContentAreaFilled(false); setFocusable(false); setForeground(TEXT_COLOR); Map underlineAttribute = new HashMap(); underlineAttribute.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); setFont(getFont().deriveFont(underlineAttribute)); } } 

如何在不删除下划线的情况下在链接中实现突出显示文本? 我是否需要将它们更改为inheritance其他内容? 提前致谢!

一种方法是对按钮文本使用HTML格式 。 当然,如果按钮看起来像按钮并且链接看起来像链接(即不是按钮),则最终用户最不会感到意外的路径。


我应该为链接inheritance其他东西吗?

对于链接,我通常使用JTextField ,如我对如何更改JButton的回答所示?

例如