在HTMLEditorKit中标记已使用的超链接

时间:2020-07-13 14:03:35

标签: java html swing hyperlink htmleditorkit

我正在使用Swing #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> int main() { FILE *fptr; int l, count = 0, index; char name[100], word[25], buffer[1000], *pos; printf("\nEnter the word to be found:"); scanf("%s", word); l = strlen(word); printf("\nEnter the file name:"); scanf("%s", name); fptr = fopen(name, "r"); if (fptr == NULL) { printf("\nProblem with opening the file"); exit(1); } while ((fgets(buffer, 1000, fptr)) != NULL) { index = 0; while ((pos = strstr(buffer + index, word)) != NULL) { index = (pos - buffer) + 1; count++; } } printf("The word %s is found %d times", word, count); fclose(fptr); } 生成HTML表。一栏显示了可选的超链接。

就像搜索引擎一样,我想标记已被调用的链接(粗体或彩色)。

添加此行为的正确位置在哪里?

编辑:

吉尔伯特,谢谢您的提示

似乎不遵守链接状态。第一行addRule不会更改颜色,而是保留默认的蓝色字体。第二个注释掉的行有效。

HTMLEditorKit

1 个答案:

答案 0 :(得分:0)

This one给了我解决方案。

稍作修改对我有用

    jEditorPane.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                HyperlinkEvent.EventType type = e.getEventType();
                
                if (type == HyperlinkEvent.EventType.ACTIVATED) {
                    
                    // mark activated
                    if (e.getSourceElement() != null) {
                        AttributeSet a = e.getSourceElement().getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(e.getSourceElement());
                        }
                    }
相关问题