如何在JtextArea中更改文本颜色? [英] How to change text color in the JtextArea?

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

问题描述

我需要知道如何做到这一点:

I need to know how to do this:

让我们说:我在jtextArea中有一个像这样的代码,

Let's say: I have a code in the jtextArea like this,

LOAD R1,1
DEC R1
STORE M,R1
ADD R4,R1,8

LOAD R1, 1 DEC R1 STORE M, R1 ADD R4, R1,8

我想要将LOAD,DEC,STORE和ADD的颜色更改为颜色BLUE
R1,R4颜色为绿色
M到RED
数字到ORANGE

I wanted to change the color of LOAD, DEC, STORE and ADD to color BLUE R1, R4 to color green M to RED numbers to ORANGE

如何更改此文字的颜色?
这些文字来自记事本,或者可以直接输入textArea。

How to change the color of this text? These text were from notepad or can be directly type to the textArea.

提前谢谢。

推荐答案

JTextArea 旨在招待纯文本。应用于单个字符的设置适用于 JTextArea 中的整个文档。但是使用 JTextPane JEditorPane 您可以选择为字符串文字着色根据您的喜好。在 JTextPane 的帮助下,您可以这样做:

JTextArea is meant to entertain Plain Text. The settings applied to a single character applies to whole of the document in JTextArea. But with JTextPane or JEditorPane you have the choice, to colour your String Literals as per your liking. Here with the help of JTextPane, you can do it like this :

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();
                }
            });
    }
}

这里是输出:

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

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