高亮链接 [英] Highlighting Links

查看:38
本文介绍了高亮链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个帮助系统,它使用链接(一个 JButton 扩展)来展开和折叠带有 JLabels 的子面板.链接和可折叠面板工作正常,但我在执行查找对话框时遇到问题.我希望能够突出显示用户搜索的部分文本.我认为我使用文本属性来为链接中的文本加下划线会干扰我突出显示文本部分的能力,但我不确定如何做.这是我的链接子类的链接类的代码:

I am creating a Help System that uses links (a JButton extension) that expand and collapse subpanels with JLabels in them. The links and the collapsible panels work, but I'm having trouble implementing my find dialog. I want to be able to highlight parts of the text for which the user searches. I think my use of text attributes to underline the text in the links is messing with my ability to highlight the parts of the text, but I'm not sure how to do it differently. Here's the code for my Link class which my links subclass:

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<TextAttribute, Integer> underlineAttribute =
        new HashMap<TextAttribute, Integer>();
    underlineAttribute.put(TextAttribute.UNDERLINE,
            TextAttribute.UNDERLINE_ON);
    setFont(getFont().deriveFont(underlineAttribute));
}

}

如何在不去掉下划线的情况下在链接中突出显示文本?我是否需要将它们更改为其他内容的子类?

How can I implement highlighting text in my links without getting rid of the underlining? Do I need to change them to subclass something else?

推荐答案

一种方法是使用 HTML 格式 用于按钮文本.当然,如果按钮看起来像按钮而链接看起来像链接(即不是按钮),那么最终用户最不会感到意外的方法是.

One approach is to use HTML formatting for the button text. Of course, the path of least surprise for the end user would be if the buttons looked like buttons and the links looked like links (i.e. not buttons).

我应该为链接子类化其他东西吗?

Should I subclass something else for the links?

对于链接,我通常使用 JTextField,如我对 如何更改JButton?

For a link I'd generally use a JTextField, as shown on my answer to How to change JButton?

这篇关于高亮链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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