在java中着色文本 [英] Coloring texts in java

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

问题描述

我有一个文件,我会逐行读。使用分割方法将每行拆分为单词,并根据单词的位置(每行的前4个字符等)以及基于单词对单词进行着色。不同的颜色应该应用于不同的词,如下所示。我想知道哪个类是有用的,我调查荧光笔。

I have a file which i will read it line by line. Split each line into words by using the split method and color the words based on their positions (first 4 chars of each line etc) and also based on the words. Different color should be applied to different words like below. I want to know which class is useful, i looked into highlighter. Any suggestions, with example would be very helpful

String text = textArea.getText();
String newLine = "\n";
String spaceDelim = "[ ]+";
String[] tokens;
String lines = text.split(newLine);
for(String line : lines) {
    tokens = line.split(spaceDelim);
    tokens[1] //should be in redColor
    tokens[2] //should be in greenColor
    tokens[3] tokens[4] //should in blueColor
}


推荐答案

如果你想让不同的文字文字有不同的颜色,则必须阅读如何使用编辑器窗格或TextPane

If you want to have different text literals to have different colours, you must read about How to use Editor Pane or TextPane. This will help you in that.

示例程序:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextPaneTest extends JFrame
{
    private JPanel topPanel;
    private JTextPane tPane;

    public TextPaneTest()
    {
        topPanel = new JPanel();        

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            

        EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));

        tPane = new JTextPane();                
        tPane.setBorder(eb);
        //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        tPane.setMargin(new Insets(5, 5, 5, 5));

        topPanel.add(tPane);

        appendToPane(tPane, "My Name is Too Good.\n", Color.RED);
        appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.BLUE);
        appendToPane(tPane, "Stack", Color.DARK_GRAY);
        appendToPane(tPane, "Over", Color.MAGENTA);
        appendToPane(tPane, "flow", Color.ORANGE);

        getContentPane().add(topPanel);

        pack();
        setVisible(true);   
    }

    private void appendToPane(JTextPane tp, String msg, Color c)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextPaneTest();
                }
            });
    }
}

这段代码的输出如下:

这篇关于在java中着色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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