SWT JTextPane 中文以及ASCII以外的字颜色高亮

本文详细介绍了如何在SWT中使用JTextPane组件,实现中文和其他非ASCII字符的高亮显示,包括设置字体颜色、背景色等技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hsbc.automation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Test window = new Test();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(606, 372);
		shell.setText("SWT Application");
		
		Composite comp = new Composite(shell, SWT.EMBEDDED);
		comp.setSize(590, 334);
		comp.setLocation(0, 0);
		
		final java.awt.Frame frame = SWT_AWT.new_Frame(comp);
		frame.setLayout(null);
		final MyTextPane editorPane = new MyTextPane();
		editorPane.setSize(590, 334);
		editorPane.setLocation(0, 0);
		
		frame.add(editorPane);
	}
}


package com.hsbc.automation;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.rtf.RTFEditorKit;

public class MyTextPane extends JTextPane {

	private static final long serialVersionUID = -8222291704834840091L;
	protected StyleContext m_context;
	protected DefaultStyledDocument m_doc;
	private MutableAttributeSet keyAttr, normalAttr;
	private MutableAttributeSet inputAttributes = new RTFEditorKit().getInputAttributes();

	public MyTextPane() {
		super();
		m_context = new StyleContext();
		m_doc = new DefaultStyledDocument(m_context);
		this.setDocument(m_doc);

		this.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
				syntaxParse();
			}
		});

		keyAttr = new SimpleAttributeSet();
		StyleConstants.setForeground(keyAttr, Color.red);

		normalAttr = new SimpleAttributeSet();
		StyleConstants.setForeground(normalAttr, Color.black);
	}

	public void syntaxParse() {
		try {
			String s = null;
			Element root = m_doc.getDefaultRootElement();

			int cursorPos = this.getCaretPosition();
			int line = root.getElementIndex(cursorPos);

			Element para = root.getElement(line);
			int start = para.getStartOffset();
			int end = para.getEndOffset() - 1;
			s = m_doc.getText(start, end - start);

			m_doc.setCharacterAttributes(start, s.length(), normalAttr, false);
			boolean flage = false;
			boolean flage2 = false;
			boolean firstFlage = true;
			for (int j = 0; j < s.length(); j++) {
				String simpleStr = s.substring(j, j+1);
				if (!isASCII(simpleStr)) {
					m_doc.setCharacterAttributes(start+j, 1, keyAttr, false);
				}
				if (simpleStr.equals("&")) {
					flage = true; 
					continue;
				}
				if (flage) {
					if (simpleStr.equals("#")) {
						flage2 = true;
						continue;
					}
				}
				
				if (flage2) {
					if (isNumber(simpleStr)) {
						if (firstFlage) {
							m_doc.setCharacterAttributes(start+j-2, 3, keyAttr, false);
							firstFlage = false;
						}else {
							m_doc.setCharacterAttributes(start+j, 1, keyAttr, false);
						}
						continue;
					}
				}
				flage = false;
				flage2 = false;
				firstFlage = true;
			}
			
			
			
			inputAttributes.addAttributes(normalAttr);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	public static boolean isASCII(int c) {  
        return c>= 0 && c < 128;
    }  

    public static boolean isASCII(String str) {  
        if (str == null) {
        	return false;  
        }
        if (str.length() == 1) {
        	return isASCII(str.charAt(0));
		}
        return false;  
    } 
    
    public static boolean isNumber(int c) {  
        return c>= 48 && c <= 57;
    }  

    public static boolean isNumber(String str) {  
        if (str == null) {
        	return false;  
        }
        if (str.length() == 1) {
        	return isNumber(str.charAt(0));
		}
        return false;  
    } 
    
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值