java - 在 jTextField 中设置突出显示颜色

标签 java swing jtextfield document swing-highlighter

我正在开发一个 Java Swing 应用程序。该应用程序允许用户使用基本命令行。例如,用户只需输入 <strong>add "Something to be added."</strong> 即可添加事件.我想强调<strong>add</strong>当用户键入此命令时。任何人都可以实现 jTextField 吗?

最佳答案

一个想法是使用 StyleDocumentDocumentFilter

enter image description here

这个其实是基于这个question ,但我不确定是否可以尝试使用 JTextField

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
import javax.swing.text.JTextComponent;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class TextFieldHighlighter {

    public static void main(String[] args) {
        new TextFieldHighlighter();
    }

    public TextFieldHighlighter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField field = new JTextField(new DefaultStyledDocument(), null, 20);

                ((AbstractDocument) field.getDocument()).setDocumentFilter(new HighlightDocumentFilter(field));
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(field);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class HighlightDocumentFilter extends DocumentFilter {

        private DefaultHighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
        private JTextComponent field;
        private SimpleAttributeSet background;

        public HighlightDocumentFilter(JTextComponent field) {
            this.field = field;
            background = new SimpleAttributeSet();
            StyleConstants.setBackground(background, Color.RED);
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
            super.insertString(fb, offset, text, attr);
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

            String match = "and";

            super.replace(fb, offset, length, text, attrs);

            int startIndex = offset - match.length();
            if (startIndex >= 0) {

                String last = fb.getDocument().getText(startIndex, match.length()).trim();
                if (last.equalsIgnoreCase(match)) {

                    field.getHighlighter().addHighlight(startIndex, startIndex + match.length(), highlightPainter);

                }

            }
        }
    }
}

关于java - 在 jTextField 中设置突出显示颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19399904/

相关文章:

Java MouseListener - 事件

java - swing java中的按钮 Action 事件不起作用?和代码查询

java - 向现有 JFrame 添加确定/取消按钮

java - 检查 JTextField 是否为数字

Java Swing JTextArea 无法正确显示

java - 有效获取对象的属性或强制声明类属性

java - Play framework java依赖注入(inject)——什么时候使用单例

java - Java程序如何保存和打开Java程序?

java - Java 中类似于 C/C++ INT_MAX 宏的是什么

Java swing 异常处理程序