java textfield 颜色_java – 在jTextField中设置hilighting颜色

一个想法是使用StyleDocument和DocumentFilter

这实际上是基于这个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);

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值