【问题标题】:Coloring texts in javajava中的文本着色
【发布时间】:2012-03-22 22:44:11
【问题描述】:

我有一个文件,我将逐行读取它。使用 split 方法将每行拆分为单词,并根据单词的位置(每行的前 4 个字符等)以及单词为单词着色。不同的颜色应该应用于不同的单词,如下所示。我想知道哪个类有用,我研究了荧光笔。任何建议,例如,都会非常有帮助

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
}

【问题讨论】:

    标签: java swing fonts colors jtextcomponent


    【解决方案1】:

    如果你想让不同的文本文字有不同的颜色,你必须阅读How to use Editor Pane or TextPane。这将对您有所帮助。

    一个示例程序:

    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 : 欢迎您,做我的客人,我能帮到你什么,我都会:-)
    • "一个示例程序 sn-p :" 奇怪的是你应该在这些词中加上 'sn-p'。我的意思是——“sn-p”通常传达“短”,但在编程环境中,它也暗示(至少对我而言)“太短而无法在没有更多代码的情况下工作”。该示例程序已按原样准备就绪。 (代码和屏幕截图+1,顺便说一句)
    【解决方案2】:

    使用JTextPaneHTMLEditorKit 添加着色标签。

    或者您可以使用JEditorPane/JTextPaneStyledEditorKit 并使用StyleConstants.setForeground() 指定文本颜色

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2012-02-28
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多