Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的文本着色_Java_Swing_Fonts_Colors_Jtextcomponent - Fatal编程技术网

java中的文本着色

java中的文本着色,java,swing,fonts,colors,jtextcomponent,Java,Swing,Fonts,Colors,Jtextcomponent,我有一个文件,我会逐行阅读。使用拆分方法将每行拆分为单词,并根据单词的位置(每行的前4个字符等)以及单词为单词上色。不同的颜色应适用于不同的词,如下面。我想知道哪个类有用,我查看了highlighter。任何建议,举个例子都会很有帮助 String text = textArea.getText(); String newLine = "\n"; String spaceDelim = "[ ]+"; String[] tokens; String lines = text.split(newL

我有一个文件,我会逐行阅读。使用拆分方法将每行拆分为单词,并根据单词的位置(每行的前4个字符等)以及单词为单词上色。不同的颜色应适用于不同的词,如下面。我想知道哪个类有用,我查看了highlighter。任何建议,举个例子都会很有帮助

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
}

使用
JTextPane
HTMLEditorKit
添加着色标记


或者您可以将
JEditorPane/JTextPane
StyledEditorKit
一起使用,并使用
StyleConstants.set前台()

指定文本颜色如果您想让不同的文本文本具有不同的颜色,则必须阅读相关内容。这将在这方面帮助你

示例程序:

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();
                }
            });
    }
}
下面是这段代码的输出:


谢谢,这就是我要找的。我会试着回去。@FirmView:欢迎您,做我的客人,无论我能帮您做什么,我都会:-)“一个示例程序片段:“奇怪的是,您应该在这些词中添加‘snippet’。我的意思是—“snippet”通常表示“short”,但在编程上下文中,它也意味着(至少对我来说)“太短,没有更多代码就无法工作”。该示例程序已准备就绪。(+1表示代码和屏幕截图,顺便说一句)