java - JEdi​​torPane为不同单词设置前景色

标签 java swing jtextpane jeditorpane

我目前正在开发一个文本编辑器,并且我有这段代码可以使特定单词的背景与其他单词不同,但我希望前景编辑颜色而不是背景。

这是我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class test {
     final static JEditorPane jep = new JEditorPane();
     static JButton button = new JButton("Refresh");
     static JTextPane jTextPane1 = new JTextPane();
     public static void main(String[] args) throws BadLocationException {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jep.setText("Hello to the public place here are the public people");
                frame.add(jep);
                frame.pack();
                frame.setSize(500, 500);
                frame.add(button,BorderLayout.NORTH);
                frame.setVisible(true);
                button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent arg0) {
                            try {
                                highlight(jep, "public");
                            } catch (BadLocationException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    }
                });
                highlight(jep, "public");
    }
     public static void highlight(JTextComponent textComp, String pattern) throws BadLocationException {
        try {

             final MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
             Highlighter hilite = textComp.getHighlighter();
             MutableAttributeSet mas = (MutableAttributeSet)new SimpleAttributeSet ();
             StyleConstants.setForeground(mas, Color.red);

            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;

            // Search for pattern
            while ((pos = text.indexOf(pattern, pos)) >= 0) {
                hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
                pos += pattern.length();

            }
        }finally{

        }
    }
}
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
    public MyHighlightPainter(Obect object) {
        super((Color) object);

    }
}

最佳答案

您需要利用编辑器StyledDocument,举个简单的例子......

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Srap {

    public static void main(String[] args) {
        JTextPane textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();

        Style style = textPane.addStyle("I'm a Style", null);
        StyleConstants.setForeground(style, Color.red);

        try {
            doc.insertString(doc.getLength(), "BLAH ", style);
        } catch (BadLocationException ex) {
        }

        StyleConstants.setForeground(style, Color.blue);

        try {
            doc.insertString(doc.getLength(), "BLEH", style);
        } catch (BadLocationException e) {
        }
    }
}

关于java - JEdi​​torPane为不同单词设置前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948148/

相关文章:

java - 如何让 JTextPane 显示省略号以表示文本溢出?

java - 在 Java 中格式化日期

java - AutoCompleteTextView选择结果后返回模型

java - 将 Jar 文件部署到 weblogic 12.1.3 时出现异常错误

java - 在 Java GUI 中查看/编辑属性文件的最佳方式?

java - 如何将按钮 Action 监听器绑定(bind)到文本字段

java - 随机数出现检查

java - 如何将另一个类的组合框的选定索引设置为 0

java - 在 JTextPane 中逐行读取文本

java - 如何在 JTextPane 中显示 Applet